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
|
#include "FindPanel.h"
#include <QHBoxLayout>
#include <QAction>
#include <QLineEdit>
#include <QKeyEvent>
#include "DictTableView.h"
#include "MainWindow.h"
FindPanel::FindPanel( MainWindow* aMainWindow ):
QWidget( aMainWindow ),
m_mainWindow( aMainWindow ), m_direction( 1 ), m_foundOnce(false)
{
m_closeButton = new QToolButton;
m_closeButton->setAutoRaise(true);
m_closeButton->setIcon(QIcon(":/images/gray-cross.png"));
m_closeButton->setToolTip(tr("Close"));
QLabel* textLabel = new QLabel(tr("Find:", "Title of the find pane"));
m_textEdit = new QComboBox;
textLabel->setBuddy(m_textEdit);
m_textEdit->setEditable(true);
m_textEdit->setInsertPolicy( QComboBox::NoInsert );
m_textEdit->setMaxCount( ComboBoxMaxItems );
m_textEdit->setMinimumWidth( TextEditMinWidth );
m_textEdit->setFocus();
m_findBackwardBtn = new QToolButton;
m_findBackwardBtn->resize(32, 32);
m_findBackwardBtn->setObjectName("backward");
m_findBackwardBtn->setIcon(QIcon(":/images/1leftarrow.png"));
m_findBackwardBtn->setToolTip(tr("Find previous"));
m_findBackwardBtn->setEnabled(false);
m_findForwardBtn = new QToolButton;
m_findForwardBtn->setObjectName("forward");
m_findForwardBtn->setIcon(QIcon(":/images/1rightarrow.png"));
m_findForwardBtn->setToolTip(tr("Find next"));
m_findForwardBtn->setEnabled(false);
m_caseSensitiveBtn = new QToolButton;
m_caseSensitiveBtn->setAutoRaise(true);
m_caseSensitiveBtn->setCheckable( true );
m_caseSensitiveBtn->setIcon(QIcon(":/images/Aa.png"));
m_caseSensitiveBtn->setToolTip(tr("Case sensitive"));
m_wholeWordsBtn = new QToolButton;
m_wholeWordsBtn->setAutoRaise(true);
m_wholeWordsBtn->setCheckable( true );
m_wholeWordsBtn->setIcon(QIcon(":/images/whole-words.png"));
m_wholeWordsBtn->setToolTip(tr("Whole words"));
m_regExpBtn = new QToolButton;
m_regExpBtn->setAutoRaise(true);
m_regExpBtn->setCheckable( true );
m_regExpBtn->setIcon(QIcon(":/images/RX.png"));
m_regExpBtn->setToolTip(tr("Regular expression"));
m_inSelectionBtn = new QToolButton;
m_inSelectionBtn->setAutoRaise(true);
m_inSelectionBtn->setCheckable( true );
m_inSelectionBtn->setIcon(QIcon(":/images/selection.png"));
m_inSelectionBtn->setToolTip(tr("In selection"));
QLabel* infoIconLbl = new QLabel;
infoIconLbl->setPixmap( QPixmap(":/images/warning.png").scaled(
16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
m_infoLbl = new QLabel(tr("String is not found"));
QHBoxLayout* infoLt = new QHBoxLayout;
infoLt->setContentsMargins( QMargins() );
infoLt->addWidget( infoIconLbl );
infoLt->addWidget( m_infoLbl );
m_infoPane = new QWidget;
m_infoPane->setLayout( infoLt );
m_infoPane->hide();
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget( m_closeButton );
mainLayout->addWidget( textLabel );
mainLayout->addWidget( m_textEdit );
mainLayout->addWidget( m_findBackwardBtn );
mainLayout->addWidget( m_findForwardBtn );
mainLayout->addWidget( m_caseSensitiveBtn );
mainLayout->addWidget( m_wholeWordsBtn );
mainLayout->addWidget( m_regExpBtn );
mainLayout->addWidget( m_inSelectionBtn );
mainLayout->addSpacing( 50 );
mainLayout->addWidget( m_infoPane );
mainLayout->addStretch();
mainLayout->setContentsMargins( QMargins() );
setLayout( mainLayout );
connect( m_inSelectionBtn, SIGNAL(toggled(bool)), SLOT(updateFindButtons()) );
connect( m_textEdit, SIGNAL(editTextChanged(const QString&)), SLOT(updateFindButtons()) );
connect( m_textEdit, SIGNAL(editTextChanged(const QString&)), m_infoPane, SLOT(hide()) );
connect( m_findForwardBtn, SIGNAL(clicked()), this, SLOT(find()) );
connect( m_findBackwardBtn, SIGNAL(clicked()), this, SLOT(find()) );
connect( m_closeButton, SIGNAL(clicked()), this, SLOT(hide()) );
}
void FindPanel::show()
{
m_textEdit->setFocus();
m_textEdit->lineEdit()->selectAll();
QWidget::show();
}
void FindPanel::keyPressEvent( QKeyEvent* event )
{
if( event->key() == Qt::Key_Return )
find();
else
QWidget::keyPressEvent( event );
}
/**
@arg aDirection 1 = forward; -1 = backward; 0 = previous direction
*/
void FindPanel::find()
{
// Check controls
QString searchText = m_textEdit->currentText();
if( searchText.isEmpty() )
return;
DictTableView* tableView = const_cast<DictTableView*>( m_mainWindow->getCurDictView() );
QModelIndexList rows = tableView->selectionModel()->selectedRows();
if( rows.size() <= 1 && m_inSelectionBtn->isChecked() )
m_inSelectionBtn->setChecked( false );
// Process search parameters
bool inSelection = m_inSelectionBtn->isChecked();
bool fromCursor = true;
if( inSelection || (rows.size() == 1 && rows.first().row() == 0 ) )
fromCursor = false;
// Process direction
if( sender() )
{
if( sender()->objectName() == "forward" )
m_direction = 1;
else if( sender()->objectName() == "backward" )
m_direction = -1;
}
// For "find again" case, the direction stays the same
// Save the entered text to combobox
int textIndex = m_textEdit->findText( searchText );
if( textIndex != 0 ) // Don't re-add the same text at the first line
{
if( textIndex > -1 ) // Remove duplicates
m_textEdit->removeItem( textIndex );
m_textEdit->insertItem( 0, searchText );
}
// Create the search regular expression
QString searchRegExpStr;
if( !m_regExpBtn->isChecked() )
searchRegExpStr = QRegExp::escape( searchText );
else
searchRegExpStr = searchText;
if( m_wholeWordsBtn->isChecked() )
searchRegExpStr = "\\b" + searchText + "\\b";
QRegExp searchRegExp = QRegExp( searchRegExpStr,
m_caseSensitiveBtn->isChecked()? Qt::CaseSensitive : Qt::CaseInsensitive);
// Get sorted search range
QModelIndexList searchRange;
if( inSelection )
{
searchRange = tableView->selectionModel()->selectedIndexes();
qSort( searchRange );
}
else // all indexes
{
QAbstractItemModel* tableModel = tableView->model();
for(int r=0; r < tableModel->rowCount(); r++)
for(int c=0; c < tableModel->columnCount(); c++)
searchRange << tableModel->index(r, c);
}
// Get the starting search point (iterator)
QListIterator<QModelIndex> startingPoint( searchRange );
if( fromCursor )
{
bool ok = startingPoint.findNext( tableView->currentIndex() );
if( !ok )
startingPoint.toFront();
if( ok && m_direction < 0 )
startingPoint.previous(); // Go one item backwards
}
else
if( m_direction < 0 )
startingPoint.toBack();
// Try to find the regexp
bool found = findRegExp( searchRegExp, startingPoint );
if ( !found && fromCursor )
{
// Continue searching
m_mainWindow->showContinueSearch();
if( m_direction > 0 )
startingPoint.toFront();
else
startingPoint.toBack();
if( findRegExp( searchRegExp, startingPoint ) )
found = true;
}
if( !found )
m_infoPane->show();
}
bool FindPanel::findRegExp( const QRegExp& aSearchRegExp, QListIterator<QModelIndex> aStartingPoint )
{
DictTableView* tableView = const_cast<DictTableView*>( m_mainWindow->getCurDictView() );
QModelIndex foundIndex;
while( (m_direction > 0)? aStartingPoint.hasNext() : aStartingPoint.hasPrevious() )
{
QModelIndex index = (m_direction > 0)? aStartingPoint.next() : aStartingPoint.previous();
QString valueStr = index.data( Qt::EditRole ).toString(); // Search in display, not edit, strings. Matters for <img>.
if( valueStr.contains( aSearchRegExp ) )
{
foundIndex = index;
break;
}
}
if( foundIndex.isValid() )
{
tableView->setFocus();
tableView->setCurrentIndex( foundIndex );
return true;
}
else
return false;
}
void FindPanel::updateFindButtons()
{
m_findForwardBtn->setEnabled( !m_textEdit->currentText().isEmpty() );
m_findBackwardBtn->setEnabled( !m_textEdit->currentText().isEmpty() && !m_inSelectionBtn->isChecked() );
}
bool FindPanel::canFindAgain()
{
if( !m_foundOnce )
return false;
const DictTableView* tableView = m_mainWindow->getCurDictView();
if( tableView && !m_textEdit->currentText().isEmpty() )
return true;
else
return false;
}
|