File: selection.cpp

package info (click to toggle)
glogg 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 832 kB
  • sloc: cpp: 6,649; sh: 66; perl: 32; sed: 25; makefile: 11
file content (173 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download
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
/*
 * Copyright (C) 2010 Nicolas Bonnefon and other contributors
 *
 * This file is part of glogg.
 *
 * glogg 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 3 of the License, or
 * (at your option) any later version.
 *
 * glogg 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 glogg.  If not, see <http://www.gnu.org/licenses/>.
 */

// This file implements Selection.
// This class implements the selection handling. No check is made on
// the validity of the selection, it must be handled by the caller.
// There are three types of selection, only one type might be active
// at any time.

#include <QtGlobal>

#include "selection.h"

Selection::Selection()
{
    selectedLine_ = -1;

    selectedPartial_.line        = -1;
    selectedPartial_.startColumn = 0;
    selectedPartial_.endColumn   = 0;

    selectedRange_.startLine = -1;
    selectedRange_.endLine   = 0;
}

void Selection::selectPortion( int line, int start_column, int end_column )
{
    // First unselect any whole line or range
    selectedLine_ = -1;
    selectedRange_.startLine = -1;

    selectedPartial_.line = line;
    selectedPartial_.startColumn = qMin ( start_column, end_column );
    selectedPartial_.endColumn   = qMax ( start_column, end_column );
}

void Selection::selectRange( int start_line, int end_line )
{
    // First unselect any whole line and portion
    selectedLine_ = -1;
    selectedPartial_.line = -1;

    selectedRange_.startLine = qMin ( start_line, end_line );
    selectedRange_.endLine   = qMax ( start_line, end_line );
}

void Selection::crop( int last_line )
{
    if ( selectedLine_ > last_line )
        selectedLine_ = -1;

    if ( selectedPartial_.line > last_line )
        selectedPartial_.line = -1;

    if ( selectedRange_.endLine > last_line )
        selectedRange_.endLine = last_line;

    if ( selectedRange_.startLine > last_line )
        selectedRange_.startLine = last_line;
};

bool Selection::getPortionForLine( int line, int* start_column, int* end_column ) const
{
    if ( selectedPartial_.line == line ) {
        *start_column = selectedPartial_.startColumn;
        *end_column   = selectedPartial_.endColumn;

        return true;
    }
    else {
        return false;
    }
}

bool Selection::isLineSelected( int line ) const
{
    if ( line == selectedLine_ )
        return true;
    else if ( selectedRange_.startLine >= 0 )
        return ( ( line >= selectedRange_.startLine )
                && ( line <= selectedRange_.endLine ) );
    else
        return false;
}

QList<int> Selection::getLines() const
{
    QList<int> selection;

    if ( selectedLine_ >= 0 )
        selection.append( selectedLine_ );
    else if ( selectedPartial_.line >= 0 )
        selection.append( selectedPartial_.line );
    else if ( selectedRange_.startLine >= 0 )
        for ( int i = selectedRange_.startLine;
                i <= selectedRange_.endLine; i++ )
            selection.append( i );

    return selection;
}

// The tab behaviour is a bit odd at the moment, full lines are not expanded
// but partials (part of line) are, they probably should not ideally.
QString Selection::getSelectedText( const AbstractLogData* logData ) const
{
    QString text;

    if ( selectedLine_ >= 0 ) {
        text = logData->getLineString( selectedLine_ );
    }
    else if ( selectedPartial_.line >= 0 ) {
        text = logData->getExpandedLineString( selectedPartial_.line ).
            mid( selectedPartial_.startColumn, ( selectedPartial_.endColumn -
                        selectedPartial_.startColumn ) + 1 );
    }
    else if ( selectedRange_.startLine >= 0 ) {
        QStringList list = logData->getLines( selectedRange_.startLine,
                selectedRange_.endLine - selectedRange_.startLine + 1 );
        text = list.join( "\n" );
    }

    return text;
}

void Selection::getNextPosition( int* line, int* column ) const
{
    *line   = 0;
    *column = 0;

    if ( selectedLine_ >= 0 ) {
        *line = selectedLine_ + 1;
    }
    else if ( selectedRange_.startLine >= 0 ) {
        *line = selectedRange_.endLine + 1;
    }
    else if ( selectedPartial_.line >= 0 ) {
        *line   = selectedPartial_.line;
        *column = selectedPartial_.endColumn + 1;
    }
}

void Selection::getPreviousPosition( int* line, int* column ) const
{
    *line   = 0;
    *column = 0;

    if ( selectedLine_ >= 0 ) {
        *line = selectedLine_;
    }
    else if ( selectedRange_.startLine >= 0 ) {
        *line = selectedRange_.startLine;
    }
    else if ( selectedPartial_.line >= 0 ) {
        *line   = selectedPartial_.line;
        *column = qMax( selectedPartial_.startColumn - 1, 0 );
    }
}