File: BoxSelection.cpp

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (559 lines) | stat: -rw-r--r-- 17,457 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program.  If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "BoxSelection.h"
#include "TextEditor.h"
#include "XmlOptions.h"

#include <QApplication>
#include <QMimeData>

static const int debugLevel = 1;

//________________________________________________________________________
const QString BoxSelection::mimeType = "text/boxSelection";

//________________________________________________________________________
BoxSelection::BoxSelection( TextEditor* parent ):
    Counter( "BoxSelection" ),
    parent_( parent )
{
    Debug::Throw( debugLevel, "BoxSelection::BoxSelection.\n" );
    updateConfiguration();
}

//________________________________________________________________________
bool BoxSelection::empty() const
{
    return
        !enabled_ ||
        state_ == State::Empty ||
        ( state_ == State::Finished && cursors_.empty() );
}

//________________________________________________________________________
void BoxSelection::synchronize( const BoxSelection& box )
{

    Debug::Throw( debugLevel, "BoxSelection::synchronize.\n" );

    // check enable state.
    if( !checkEnabled() ) return;

    begin_ = box.begin();
    end_ = box.end();
    state_ = box.state();

    QRect old( rect() );
    _updateRect();

    parent_->viewport()->update( parent_->toViewport( old.united( rect() ) ).adjusted( -2, -2, 3, 3 ) );

    return;

}


//________________________________________________________________________
void BoxSelection::updateConfiguration()
{

    Debug::Throw( "BoxSelection::updateConfiguration.\n" );

    // retrieve box selection color from options
    QColor color = parent_->palette().color( QPalette::Active, QPalette::Highlight );
    color_ = color;

    // retrieve shading from options
    color.setAlphaF( XmlOptions::get().get<double>("BOX_SELECTION_ALPHA")/100 );
    brush_ = QBrush( color );

    // additional initialization dependening on whether box selection is enabled or not
    enabled_ = false;
    checkEnabled();
}

//________________________________________________________________________
bool BoxSelection::checkEnabled()
{

    Debug::Throw( debugLevel, "BoxSelection::checkEnabled.\n" );
    if( isEnabled() ) return true;

    /*
    check if color is valid and font is fixed pitched
    to not modify the previous attributes if disabled
    because they are needed to clear any existing selection
    */
    bool fixed( QFontInfo( parent_->font() ).fixedPitch() );
    enabled_ = fixed && color_.isValid();
    Debug::Throw( debugLevel ) << "BoxSelection::updateConfiguration - isEnabled: " << isEnabled() << endl;
    if( !isEnabled() ) return false;

    // read font attributes
    fontWidth_ = parent_->fontMetrics().width( " " );
    fontHeight_ = parent_->fontMetrics().height();

    return true;
}

//________________________________________________________________________
bool BoxSelection::start( QPoint point )
{
    Debug::Throw( debugLevel, "BoxSelection::start.\n" );
    if( state_ != State::Empty ) return false;

    // store point
    begin_ = end_ = parent_->fromViewport( point );

    // adjust rectangle size
    _updateRect();

    // set parent cursor
    parent_->setTextCursor( parent_->cursorForPosition( cursor_ ) );

    state_ = State::Started;
    return true;
}

//________________________________________________________________________
bool BoxSelection::update( QPoint point )
{
    Debug::Throw( debugLevel, "BoxSelection::update.\n" );
    if( state_ != State::Started ) return false;

    // store end point
    end_ = parent_->fromViewport( point );

    // retrieve old rect
    QRect old( rect() );

    // update with new value
    _updateRect();

    // update parent
    // the adjustment is to account for the pen width
    parent_->viewport()->update( parent_->toViewport( old.united( rect() ).adjusted( -2, -2, 3, 3 ) ) );
    parent_->setTextCursor( parent_->cursorForPosition( cursor_ ) );

    return true;
}

//________________________________________________________________________
bool BoxSelection::finish( QPoint point )
{
    Debug::Throw( debugLevel, "BoxSelection::finish.\n" );
    if( state_ != State::Started ) return false;

    update( point );
    state_ = State::Finished;

    // store and copy to clipboard
    _store();
    toClipboard( QClipboard::Selection );

    return true;
}

//________________________________________________________________________
bool BoxSelection::clear()
{
    Debug::Throw( debugLevel, "BoxSelection::clear.\n" );
    if( state_ != State::Finished ) return false;

    // change state and redraw
    state_ = State::Empty;

    // update parent
    parent_->viewport()->update( parent_->toViewport( rect() ).adjusted( -2, -2, 3, 3 ) );

    // clear cursors points and rect
    cursors_.clear();
    cursor_ = begin_ = end_ = QPoint();
    rect_ = QRect();

    return true;
}


//________________________________________________________________________
QString BoxSelection::toString() const
{
    Debug::Throw( debugLevel, "BoxSelection::toString.\n" );

    QString out;

    // retrieve cursor, copy selected text in output string
    CursorList cursors( cursorList() );
    if( cursorList().empty() ) return out;

    // append a new line for all items but the last
    for( int i=0;  i < cursorList().size()-1; i++ )
    { out += cursorList()[i].selectedText().leftJustified( cursors.columnCount() ) + "\n"; }

    // append last item
    out += cursorList().back().selectedText().leftJustified( cursors.columnCount() );

    return out;

}

//________________________________________________________________________
bool BoxSelection::fromString( QString input )
{

    Debug::Throw( debugLevel, "BoxSelection::fromString.\n" );

    // check state
    if( state() != State::Finished ) return false;

    // replace all tab characters by emulated tabs
    input.replace( "\t", parent_->emulatedTabCharacter() );

    // try split
    QStringList inputList( input.split( "\n" ) );

    // retrieve maximum length
    int columns(0);
    for( int i=0; i < inputList.size(); i++ )
    { columns = qMax( columns, inputList[i].length() ); }

    // if there are more lines in current box than in the selection, fill with blank lines
    for( int i = inputList.size(); i < cursors_.size(); i++ )
    { inputList << QString( columns, ' ' ); }

    // loop over items and cursors, replace strings
    QTextCursor cursor( parent_->textCursor() );
    cursor.beginEditBlock();
    for( int i=0; i < qMin( inputList.size(), cursors_.size() ); i++ )
    {

        cursor.setPosition( cursors_[i].anchor() );

        // if cursor has no selection, one need to make sur that there is enough white spaces in the line before copying the new string
        // this hack is necessary because for some reason cursor.movePosition( QTextCursor::StartOfLine ) fails.
        int extra_length = cursors_.firstColumn() ;
        int old_bottom( parent_->cursorRect( cursor ).bottom() );
        int new_bottom( 0 );
        while( cursor.movePosition( QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor ) && (new_bottom =  parent_->cursorRect( cursor ).bottom() ) == old_bottom )
        {
            extra_length--;
            old_bottom = new_bottom;
        }

        // move to current cursor end of the selection
        cursor.setPosition( cursors_[i].position(), QTextCursor::KeepAnchor );

        // insert new text
        cursor.insertText( inputList[i].rightJustified( extra_length + inputList[i].size() ).leftJustified( extra_length + columns ) );

    }

    // if there are more lines in inputList than in current selection, insert new lines
    for( int i = cursors_.size(); i < inputList.size(); i++ )
    {
        cursor.insertText( QString( '\n' ) +
            inputList[i]
            .rightJustified( cursors_.firstColumn() + inputList[i].size() )
            .leftJustified( cursors_.firstColumn() + columns ) );
    }

    cursor.endEditBlock();
    parent_->setTextCursor( cursor );

    return true;

}

//________________________________________________________________________
bool BoxSelection::toClipboard( const QClipboard::Mode& mode ) const
{
    Debug::Throw(debugLevel) << "BoxSelection::toClipboard - mode: " << ( mode == QClipboard::Selection ? "Selection":"Clipboard" ) << endl;

    // check if selection mode is available
    if( mode == QClipboard::Selection && !qApp->clipboard()->supportsSelection() ) return false;

    // check if state is ok
    if( state() != State::Finished ) return false;

    // check if stored text makes sense
    QString selection( toString() );
    if( selection.isEmpty() ) return false;

    // store text into MimeData
    QMimeData *data( new QMimeData );
    data->setText( selection );
    data->setData( mimeType, qPrintable( selection ) );

    // copy selected text to clipboard
    qApp->clipboard()->setMimeData( data, mode );

    return true;

}

//________________________________________________________________________
bool BoxSelection::fromClipboard( const QClipboard::Mode& mode )
{

    Debug::Throw( debugLevel, "BoxSelection::fromClipboard.\n" );

    // check mode
    if( mode == QClipboard::Selection && !qApp->clipboard()->supportsSelection() ) return false;

    // check mime data availability
    const QMimeData* data( qApp->clipboard()->mimeData() );
    if( !data )
    {
        Debug::Throw(debugLevel) << "BoxSelection::fromClipboard - no mimeData" << endl;
        return false;
    }

    // try retrieve directly from formated input
    return data->hasText() ? fromString( data->text() ) : false;

}

//________________________________________________________________________
bool BoxSelection::removeSelectedText() const
{
    Debug::Throw( debugLevel, "BoxSelection::removeSelectedText.\n" );

    // check if state is ok
    if( state() != State::Finished || cursorList().empty() ) return false;

    QTextCursor stored( parent_->textCursor() );
    QTextCursor cursor( parent_->textCursor() );
    cursor.beginEditBlock();
    for( const auto& savedCursor:cursorList() )
    {

        // select line and remove
        cursor.setPosition( savedCursor.anchor() );
        cursor.setPosition( savedCursor.position(), QTextCursor::KeepAnchor );
        cursor.removeSelectedText();

    }

    cursor.endEditBlock();

    // restore cursor
    if( stored.position() == cursorList().front().position() || stored.position() == cursorList().front().anchor() )
    {
        stored.setPosition( cursorList().front().anchor() );
    } else stored.setPosition( cursor.position() );

    parent_->setTextCursor( stored );

    return true;

}

//________________________________________________________________________
bool BoxSelection::toUpper()
{
    Debug::Throw( debugLevel, "BoxSelection::toUpper.\n" );

    // check if state is ok
    if( state() != State::Finished || cursorList().empty() ) return false;

    QTextCursor stored( parent_->textCursor() );
    QTextCursor cursor( parent_->textCursor() );
    cursor.beginEditBlock();
    for( const auto& savedCursor:cursorList() )
    {

        // select line and remove
        cursor.setPosition( savedCursor.anchor() );
        cursor.setPosition( savedCursor.position(), QTextCursor::KeepAnchor );
        cursor.insertText( cursor.selectedText().toUpper() );

    }

    cursor.endEditBlock();

    // restore cursor
    if( stored.position() == cursorList().front().position() || stored.position() == cursorList().front().anchor() )
    {
        stored.setPosition( cursorList().front().anchor() );
    } else stored.setPosition( cursor.position() );

    parent_->setTextCursor( stored );

    _store();
    toClipboard( QClipboard::Selection );

    return true;

}

//________________________________________________________________________
bool BoxSelection::toLower()
{
    Debug::Throw( debugLevel, "BoxSelection::toLower.\n" );

    // check if state is ok
    if( state() != State::Finished || cursorList().empty() ) return false;

    QTextCursor stored( parent_->textCursor() );
    QTextCursor cursor( parent_->textCursor() );
    cursor.beginEditBlock();
    for( const auto& savedCursor:cursorList() )
    {

        // select line and remove
        cursor.setPosition( savedCursor.anchor() );
        cursor.setPosition( savedCursor.position(), QTextCursor::KeepAnchor );
        cursor.insertText( cursor.selectedText().toLower() );

    }

    cursor.endEditBlock();

    // restore cursor
    if( stored.position() == cursorList().front().position() || stored.position() == cursorList().front().anchor() )
    {
        stored.setPosition( cursorList().front().anchor() );
    } else stored.setPosition( cursor.position() );

    parent_->setTextCursor( stored );

    _store();
    toClipboard( QClipboard::Selection );
    return true;

}

//________________________________________________________________________
bool BoxSelection::mergeCharFormat( const QTextCharFormat& format ) const
{

    Debug::Throw( debugLevel, "BoxSelection::mergeCharFormat.\n" );

    // trailing space regexp
    static QRegExp regexp( "\\s+$" );

    // check if state is ok
    if( state() != State::Finished || cursorList().empty() ) return false;

    QTextCursor stored( parent_->textCursor() );
    QTextCursor cursor( parent_->textCursor() );
    cursor.beginEditBlock();
    for( const auto& savedCursor:cursorList() )
    {

        // select line and remove
        cursor.setPosition( savedCursor.anchor() );
        cursor.setPosition( savedCursor.position(), QTextCursor::KeepAnchor );

        // get selection, look for trailing spaces
        QString text( cursor.selectedText() );
        if( regexp.indexIn( text ) >= 0 )
        { cursor.movePosition( QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, regexp.matchedLength() ); }

        cursor.mergeCharFormat( format );

    }

    cursor.endEditBlock();

    // restore cursor
    if( stored.position() == cursorList().front().position() || stored.position() == cursorList().front().anchor() )
    {
        stored.setPosition( cursorList().front().anchor() );
    } else stored.setPosition( cursor.position() );

    parent_->setTextCursor( stored );

    return true;

}

//________________________________________________________________________
void BoxSelection::_updateRect()
{
    Debug::Throw( debugLevel, "BoxSelection::_updateRect.\n" );
    int xMin( qMin( begin_.x(), end_.x() ) );
    int xMax( qMax( begin_.x(), end_.x() ) );

    int yMin( qMin( begin_.y(), end_.y() ) );
    int yMax( qMax( begin_.y(), end_.y() ) );

    QPoint begin( xMin - (xMin%fontWidth_) + 2, yMin - (yMin%fontHeight_) + 2 );
    QPoint end( xMax - (xMax%fontWidth_) + 1, yMax + fontHeight_ - (yMax%fontHeight_) );

    // decide location of cursor point
    cursor_.setX( begin_.x() < end_.x() ? end.x() : begin.x() );
    cursor_.setY( begin_.y() < end_.y() ?  end.y() : begin.y() + fontHeight_ - 1 );
    cursor_ = parent_->toViewport( cursor_ );

    // compute rect
    rect_ = QRect( begin, end );

    return;

}

//________________________________________________________________________
void BoxSelection::_store()
{
    Debug::Throw( debugLevel, "BoxSelection::_store.\n" );

    // retrieve box selection size
    int firstColumn = rect().left() / fontWidth_;
    int columns = rect().width() / fontWidth_;
    int rows = rect().height() / fontHeight_ + 1;

    Debug::Throw() << "BoxSelection::_store - [" << firstColumn << "," << columns << "," << rows << "]" << endl;

    // translate rect
    QRect local( parent_->toViewport( rect() ) );
    cursors_ = CursorList( firstColumn, columns );
    for( int row = 0; row < rows; row ++ )
    {

        // vertical offset for this row
        QPoint voffset( 0, (int)(fontHeight_*( 0.5+row )) );
        QPoint begin( local.topLeft() + voffset );
        QPoint end( local.topRight() + voffset );

        Debug::Throw() << "BoxSelection::_store -"
            << " begin: (" << begin.x() << "," << begin.y() << ")"
            << " end: (" << end.x() << "," << end.y() << ")"
            << endl;

        QTextCursor cursor( parent_->cursorForPosition( begin ) );
        Debug::Throw()
            << "BoxSelection::_store -"
            << " begin: " << parent_->cursorForPosition( begin ).position()
            << " end: " << parent_->cursorForPosition( end ).position()
            << endl;

        // check if cursor is at end of block, and move at end
        if( !cursor.atBlockEnd() )
        { cursor.setPosition( parent_->cursorForPosition( end ).position(), QTextCursor::KeepAnchor ); }

        // store in list
        cursors_ << cursor;

    }

    return;

}