xref: /glogg/src/filteredview.cpp (revision 8e78820269301b7bc20854ff27cb7da6d674ec04)
1 /*
2  * Copyright (C) 2009, 2010, 2012, 2017 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 // This file implements the FilteredView concrete class.
21 // Most of the actual drawing and event management is done in AbstractLogView
22 // Only behaviour specific to the filtered (bottom) view is implemented here.
23 
24 #include <cassert>
25 
26 #include "filteredview.h"
27 
28 FilteredView::FilteredView( LogFilteredData* newLogData,
29         const QuickFindPattern* const quickFindPattern, QWidget* parent )
30     : AbstractLogView( newLogData, quickFindPattern, parent )
31 {
32     // We keep a copy of the filtered data for fast lookup of the line type
33     logFilteredData_ = newLogData;
34 }
35 
36 void FilteredView::setVisibility( Visibility visi )
37 {
38     assert( logFilteredData_ );
39 
40     LogFilteredData::Visibility data_visibility =
41         LogFilteredData::MarksAndMatches;
42     switch ( visi ) {
43         case MarksOnly:
44             data_visibility = LogFilteredData::MarksOnly;
45             break;
46         case MatchesOnly:
47             data_visibility = LogFilteredData::MatchesOnly;
48             break;
49         case MarksAndMatches:
50             data_visibility = LogFilteredData::MarksAndMatches;
51             break;
52     };
53 
54     logFilteredData_->setVisibility( data_visibility );
55 
56     updateData();
57 }
58 
59 // For the filtered view, a line is always matching!
60 AbstractLogView::LineType FilteredView::lineType( int lineNumber ) const
61 {
62     LogFilteredData::FilteredLineType type =
63         logFilteredData_->filteredLineTypeByIndex( lineNumber );
64     if ( type == LogFilteredData::Mark )
65         return Marked;
66     else
67         return Match;
68 }
69 
70 qint64 FilteredView::displayLineNumber( int lineNumber ) const
71 {
72     // Display a 1-based index
73     return logFilteredData_->getMatchingLineNumber( lineNumber ) + 1;
74 }
75 
76 qint64 FilteredView::maxDisplayLineNumber() const
77 {
78     return logFilteredData_->getNbTotalLines();
79 }
80 
81 void FilteredView::keyPressEvent( QKeyEvent* keyEvent )
82 {
83     bool shiftModifier = (keyEvent->modifiers() & Qt::ShiftModifier) == Qt::ShiftModifier;
84     bool noModifier = keyEvent->modifiers() == Qt::NoModifier;
85 
86     if ( keyEvent->key() == Qt::Key_BracketLeft && noModifier ) {
87         for ( qint64 i = static_cast<qint64>( getViewPosition() ) - 1; i >= 0; --i ) {
88             if ( lineType( i ) == Marked ) {
89                 selectAndDisplayLine( static_cast<LineNumber>( i ) );
90                 break;
91             }
92         }
93         keyEvent->accept();
94     }
95     else if ( keyEvent->key() == Qt::Key_BracketRight && noModifier ) {
96         for ( qint64 i = getViewPosition() + 1;
97                 i < logFilteredData_->getNbLine(); ++i ) {
98             if ( lineType( i ) == Marked ) {
99                 selectAndDisplayLine( static_cast<LineNumber>( i ) );
100                 break;
101             }
102         }
103         keyEvent->accept();
104     }
105     else {
106         keyEvent->ignore();
107         AbstractLogView::keyPressEvent( keyEvent );
108     }
109 }
110