File: quickfindpattern.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 (116 lines) | stat: -rw-r--r-- 3,178 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
/*
 * 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 QuickFindPattern.
// This class implements part of the Quick Find mechanism, it only stores the
// current search pattern, once it has been confirmed (return pressed),
// it can be asked to return the matches in a specific string.

#include "quickfindpattern.h"

#include "persistentinfo.h"
#include "configuration.h"

QuickFindPattern::QuickFindPattern() : QObject(), regexp_()
{
    active_ = false;
}

void QuickFindPattern::changeSearchPattern( const QString& pattern )
{
    // Determine the type of regexp depending on the config
    QRegExp::PatternSyntax syntax;
    switch ( Persistent<Configuration>( "settings" ).quickfindRegexpType() ) {
        case Wildcard:
            syntax = QRegExp::Wildcard;
            break;
        case FixedString:
            syntax = QRegExp::FixedString;
            break;
        default:
            syntax = QRegExp::RegExp2;
            break;
    }

    regexp_.setPattern( pattern );
    regexp_.setPatternSyntax( syntax );

    if ( regexp_.isValid() && ( ! regexp_.isEmpty() ) )
        active_ = true;
    else
        active_ = false;

    emit patternUpdated();
}

bool QuickFindPattern::matchLine( const QString& line,
        QList<QuickFindMatch>& matches ) const
{
    matches.clear();

    if ( active_ ) {
        int pos = 0;
        while ( ( pos = regexp_.indexIn( line, pos ) ) != -1 ) {
            int length = regexp_.matchedLength();
            matches << QuickFindMatch( pos, length );
            pos += length;
        }
    }

    return ( matches.length() > 0 );
}

bool QuickFindPattern::isLineMatching( const QString& line, int column ) const
{
    int pos = 0;

    if ( ! active_ )
        return false;
    if ( ( pos = regexp_.indexIn( line, column ) ) != -1 ) {
        lastMatchStart_ = pos;
        lastMatchEnd_   = pos + regexp_.matchedLength() - 1;

        return true;
    }
    else
        return false;
}

bool QuickFindPattern::isLineMatchingBackward(
        const QString& line, int column ) const
{
    int pos = 0;

    if ( ! active_ )
        return false;
    if ( ( pos = regexp_.lastIndexIn( line, column ) ) != -1 ) {
        lastMatchStart_ = pos;
        lastMatchEnd_   = pos + regexp_.matchedLength() - 1;

        return true;
    }
    else
        return false;
}

void QuickFindPattern::getLastMatch( int* start_col, int* end_col ) const
{
    *start_col = lastMatchStart_;
    *end_col   = lastMatchEnd_;
}