/
EditorView.cpp
854 lines (746 loc) · 23.6 KB
/
EditorView.cpp
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/***************************************************************************
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
# include <QCheckBox>
# include <QClipboard>
# include <QDateTime>
# include <QHBoxLayout>
# include <QVBoxLayout>
# include <QLineEdit>
# include <QMessageBox>
# include <QPrinter>
# include <QPrintDialog>
# include <QPlainTextEdit>
# include <QPrintPreviewDialog>
# include <QSpacerItem>
# include <QStyle>
# if QT_VERSION < 0x060000
# include <QTextCodec>
# endif
# include <QTextCursor>
# include <QTextDocument>
# include <QTextStream>
# include <QTimer>
# include <QToolButton>
#endif
#include "EditorView.h"
#include "Application.h"
#include "FileDialog.h"
#include "Macro.h"
#include "MainWindow.h"
#include "PythonEditor.h"
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <Base/Parameter.h>
using namespace Gui;
namespace Gui {
class EditorViewP {
public:
QPlainTextEdit* textEdit;
SearchBar* searchBar;
QString fileName;
EditorView::DisplayName displayName;
QTimer* activityTimer;
uint timeStamp;
bool lock;
bool aboutToClose;
QStringList undos;
QStringList redos;
};
}
// -------------------------------------------------------
/* TRANSLATOR Gui::EditorView */
TYPESYSTEM_SOURCE_ABSTRACT(Gui::EditorView, Gui::MDIView)
/**
* Constructs a EditorView which is a child of 'parent', with the
* name 'name'.
*/
EditorView::EditorView(QPlainTextEdit* editor, QWidget* parent)
: MDIView(nullptr,parent,Qt::WindowFlags()), WindowParameter( "Editor" )
{
d = new EditorViewP;
d->lock = false;
d->aboutToClose = false;
d->displayName = EditorView::FullName;
// create the editor first
d->textEdit = editor;
d->textEdit->setLineWrapMode(QPlainTextEdit::NoWrap);
d->searchBar = new SearchBar();
d->searchBar->setEditor(editor);
// update editor actions on request
Gui::MainWindow* mw = Gui::getMainWindow();
connect(editor, SIGNAL(undoAvailable(bool)), mw, SLOT(updateEditorActions()));
connect(editor, SIGNAL(redoAvailable(bool)), mw, SLOT(updateEditorActions()));
connect(editor, SIGNAL(copyAvailable(bool)), mw, SLOT(updateEditorActions()));
connect(editor, SIGNAL(showSearchBar()), d->searchBar, SLOT(activate()));
connect(editor, SIGNAL(findNext()), d->searchBar, SLOT(findNext()));
connect(editor, SIGNAL(findPrevious()), d->searchBar, SLOT(findPrevious()));
// Create the layout containing the workspace and a tab bar
auto hbox = new QFrame(this);
hbox->setFrameShape(QFrame::StyledPanel);
hbox->setFrameShadow(QFrame::Sunken);
auto layout = new QVBoxLayout();
layout->setMargin(1);
layout->addWidget(d->textEdit);
layout->addWidget(d->searchBar);
d->textEdit->setParent(hbox);
d->searchBar->setParent(hbox);
hbox->setLayout(layout);
setCentralWidget(hbox);
setCurrentFileName(QString());
d->textEdit->setFocus();
setWindowIcon(d->textEdit->windowIcon());
ParameterGrp::handle hPrefGrp = getWindowParameter();
hPrefGrp->Attach( this );
hPrefGrp->NotifyAll();
d->activityTimer = new QTimer(this);
connect(d->activityTimer, SIGNAL(timeout()),
this, SLOT(checkTimestamp()) );
connect(d->textEdit->document(), SIGNAL(modificationChanged(bool)),
this, SLOT(setWindowModified(bool)));
connect(d->textEdit->document(), SIGNAL(undoAvailable(bool)),
this, SLOT(undoAvailable(bool)));
connect(d->textEdit->document(), SIGNAL(redoAvailable(bool)),
this, SLOT(redoAvailable(bool)));
connect(d->textEdit->document(), SIGNAL(contentsChange(int, int, int)),
this, SLOT(contentsChange(int, int, int)));
}
/** Destroys the object and frees any allocated resources */
EditorView::~EditorView()
{
d->activityTimer->stop();
delete d->activityTimer;
delete d;
getWindowParameter()->Detach( this );
}
QPlainTextEdit* EditorView::getEditor() const
{
return d->textEdit;
}
void EditorView::showEvent(QShowEvent* event)
{
Gui::MainWindow* mw = Gui::getMainWindow();
mw->updateEditorActions();
MDIView::showEvent(event);
}
void EditorView::hideEvent(QHideEvent* event)
{
MDIView::hideEvent(event);
}
void EditorView::closeEvent(QCloseEvent* event)
{
MDIView::closeEvent(event);
if (event->isAccepted()) {
d->aboutToClose = true;
Gui::MainWindow* mw = Gui::getMainWindow();
mw->updateEditorActions();
}
}
void EditorView::OnChange(Base::Subject<const char*> &rCaller,const char* rcReason)
{
Q_UNUSED(rCaller);
ParameterGrp::handle hPrefGrp = getWindowParameter();
if (strcmp(rcReason, "EnableLineNumber") == 0) {
//bool show = hPrefGrp->GetBool( "EnableLineNumber", true );
}
}
void EditorView::checkTimestamp()
{
QFileInfo fi(d->fileName);
uint timeStamp = fi.lastModified().toTime_t();
if (timeStamp != d->timeStamp) {
switch( QMessageBox::question( this, tr("Modified file"),
tr("%1.\n\nThis has been modified outside of the source editor. Do you want to reload it?").arg(d->fileName),
QMessageBox::Yes|QMessageBox::Default, QMessageBox::No|QMessageBox::Escape) )
{
case QMessageBox::Yes:
// updates time stamp and timer
open( d->fileName );
return;
case QMessageBox::No:
d->timeStamp = timeStamp;
break;
}
}
d->activityTimer->setSingleShot(true);
d->activityTimer->start(3000);
}
/**
* Runs the action specified by \a pMsg.
*/
bool EditorView::onMsg(const char* pMsg,const char** /*ppReturn*/)
{
// don't allow any actions if the editor is being closed
if (d->aboutToClose)
return false;
if (strcmp(pMsg, "Save") == 0) {
saveFile();
return true;
}
else if (strcmp(pMsg, "SaveAs") == 0) {
saveAs();
return true;
}
else if (strcmp(pMsg, "Cut") == 0) {
cut();
return true;
}
else if (strcmp(pMsg, "Copy") == 0) {
copy();
return true;
}
else if (strcmp(pMsg, "Paste") == 0) {
paste();
return true;
}
else if (strcmp(pMsg, "Undo") == 0) {
undo();
return true;
}
else if (strcmp(pMsg, "Redo") == 0) {
redo();
return true;
}
else if (strcmp(pMsg, "ViewFit") == 0) {
// just ignore this
return true;
}
return false;
}
/**
* Checks if the action \a pMsg is available. This is for enabling/disabling
* the corresponding buttons or menu items for this action.
*/
bool EditorView::onHasMsg(const char* pMsg) const
{
// don't allow any actions if the editor is being closed
if (d->aboutToClose)
return false;
if (strcmp(pMsg, "Run") == 0)
return true;
if (strcmp(pMsg, "DebugStart") == 0)
return true;
if (strcmp(pMsg, "DebugStop") == 0)
return true;
if (strcmp(pMsg, "SaveAs") == 0)
return true;
if (strcmp(pMsg, "Print") == 0)
return true;
if (strcmp(pMsg, "PrintPreview") == 0)
return true;
if (strcmp(pMsg, "PrintPdf") == 0)
return true;
if (strcmp(pMsg, "Save") == 0) {
return d->textEdit->document()->isModified();
}
else if (strcmp(pMsg, "Cut") == 0) {
bool canWrite = !d->textEdit->isReadOnly();
return (canWrite && (d->textEdit->textCursor().hasSelection()));
}
else if (strcmp(pMsg, "Copy") == 0) {
return ( d->textEdit->textCursor().hasSelection() );
}
else if (strcmp(pMsg, "Paste") == 0) {
QClipboard *cb = QApplication::clipboard();
QString text;
// Copy text from the clipboard (paste)
text = cb->text();
bool canWrite = !d->textEdit->isReadOnly();
return ( !text.isEmpty() && canWrite );
}
else if (strcmp(pMsg, "Undo") == 0) {
return d->textEdit->document()->isUndoAvailable ();
}
else if (strcmp(pMsg, "Redo") == 0) {
return d->textEdit->document()->isRedoAvailable ();
}
return false;
}
/** Checking on close state. */
bool EditorView::canClose()
{
if ( !d->textEdit->document()->isModified() )
return true;
this->setFocus(); // raises the view to front
switch( QMessageBox::question(this, tr("Unsaved document"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes|QMessageBox::Default, QMessageBox::No,
QMessageBox::Cancel|QMessageBox::Escape))
{
case QMessageBox::Yes:
return saveFile();
case QMessageBox::No:
return true;
case QMessageBox::Cancel:
return false;
default:
return false;
}
}
void EditorView::setDisplayName(EditorView::DisplayName type)
{
d->displayName = type;
}
/**
* Saves the content of the editor to a file specified by the appearing file dialog.
*/
bool EditorView::saveAs()
{
QString fn = FileDialog::getSaveFileName(this, QObject::tr("Save Macro"),
QString(), QString::fromLatin1("%1 (*.FCMacro);;Python (*.py)").arg(tr("FreeCAD macro")));
if (fn.isEmpty())
return false;
setCurrentFileName(fn);
return saveFile();
}
/**
* Opens the file \a fileName.
*/
bool EditorView::open(const QString& fileName)
{
if (!QFile::exists(fileName))
return false;
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
return false;
d->lock = true;
d->textEdit->setPlainText(QString::fromUtf8(file.readAll()));
d->lock = false;
d->undos.clear();
d->redos.clear();
file.close();
QFileInfo fi(fileName);
d->timeStamp = fi.lastModified().toTime_t();
d->activityTimer->setSingleShot(true);
d->activityTimer->start(3000);
setCurrentFileName(fileName);
return true;
}
/**
* Copies the selected text to the clipboard and deletes it from the text edit.
* If there is no selected text nothing happens.
*/
void EditorView::cut()
{
d->textEdit->cut();
}
/**
* Copies any selected text to the clipboard.
*/
void EditorView::copy()
{
d->textEdit->copy();
}
/**
* Pastes the text from the clipboard into the text edit at the current cursor position.
* If there is no text in the clipboard nothing happens.
*/
void EditorView::paste()
{
d->textEdit->paste();
}
/**
* Undoes the last operation.
* If there is no operation to undo, i.e. there is no undo step in the undo/redo history, nothing happens.
*/
void EditorView::undo()
{
d->lock = true;
if (!d->undos.isEmpty()) {
d->redos << d->undos.back();
d->undos.pop_back();
}
d->textEdit->document()->undo();
d->lock = false;
}
/**
* Redoes the last operation.
* If there is no operation to undo, i.e. there is no undo step in the undo/redo history, nothing happens.
*/
void EditorView::redo()
{
d->lock = true;
if (!d->redos.isEmpty()) {
d->undos << d->redos.back();
d->redos.pop_back();
}
d->textEdit->document()->redo();
d->lock = false;
}
/**
* Shows the printer dialog.
*/
void EditorView::print()
{
QPrinter printer(QPrinter::ScreenResolution);
printer.setFullPage(true);
QPrintDialog dlg(&printer, this);
if (dlg.exec() == QDialog::Accepted) {
d->textEdit->document()->print(&printer);
}
}
void EditorView::printPreview()
{
QPrinter printer(QPrinter::ScreenResolution);
QPrintPreviewDialog dlg(&printer, this);
connect(&dlg, SIGNAL(paintRequested (QPrinter *)),
this, SLOT(print(QPrinter *)));
dlg.exec();
}
void EditorView::print(QPrinter* printer)
{
d->textEdit->document()->print(printer);
}
/**
* Prints the document into a Pdf file.
*/
void EditorView::printPdf()
{
QString filename = FileDialog::getSaveFileName(this, tr("Export PDF"), QString(),
QString::fromLatin1("%1 (*.pdf)").arg(tr("PDF file")));
if (!filename.isEmpty()) {
QPrinter printer(QPrinter::ScreenResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(filename);
d->textEdit->document()->print(&printer);
}
}
void EditorView::setCurrentFileName(const QString &fileName)
{
d->fileName = fileName;
Q_EMIT changeFileName(d->fileName);
d->textEdit->document()->setModified(false);
QString name;
QFileInfo fi(fileName);
switch (d->displayName) {
case FullName:
name = fileName;
break;
case FileName:
name = fi.fileName();
break;
case BaseName:
name = fi.baseName();
break;
}
QString shownName;
if (fileName.isEmpty())
shownName = tr("untitled[*]");
else
shownName = QString::fromLatin1("%1[*]").arg(name);
shownName += tr(" - Editor");
setWindowTitle(shownName);
setWindowModified(false);
}
QString EditorView::fileName() const
{
return d->fileName;
}
/**
* Saves the contents to a file.
*/
bool EditorView::saveFile()
{
if (d->fileName.isEmpty())
return saveAs();
QFile file(d->fileName);
if (!file.open(QFile::WriteOnly))
return false;
QTextStream ts(&file);
#if QT_VERSION < 0x060000
ts.setCodec(QTextCodec::codecForName("UTF-8"));
#endif
ts << d->textEdit->document()->toPlainText();
file.close();
d->textEdit->document()->setModified(false);
QFileInfo fi(d->fileName);
d->timeStamp = fi.lastModified().toTime_t();
return true;
}
void EditorView::undoAvailable(bool undo)
{
if (!undo)
d->undos.clear();
}
void EditorView::redoAvailable(bool redo)
{
if (!redo)
d->redos.clear();
}
void EditorView::contentsChange(int position, int charsRemoved, int charsAdded)
{
Q_UNUSED(position);
if (d->lock)
return;
if (charsRemoved > 0 && charsAdded > 0)
return; // syntax highlighting
else if (charsRemoved > 0)
d->undos << tr("%1 chars removed").arg(charsRemoved);
else if (charsAdded > 0)
d->undos << tr("%1 chars added").arg(charsAdded);
else
d->undos << tr("Formatted");
d->redos.clear();
}
/**
* Get the undo history.
*/
QStringList EditorView::undoActions() const
{
return d->undos;
}
/**
* Get the redo history.
*/
QStringList EditorView::redoActions() const
{
return d->redos;;
}
void EditorView::focusInEvent (QFocusEvent *)
{
d->textEdit->setFocus();
}
// ---------------------------------------------------------
TYPESYSTEM_SOURCE_ABSTRACT(Gui::PythonEditorView, Gui::EditorView)
PythonEditorView::PythonEditorView(PythonEditor* editor, QWidget* parent)
: EditorView(editor, parent), _pye(editor)
{
connect(this, SIGNAL(changeFileName(const QString&)),
editor, SLOT(setFileName(const QString&)));
}
PythonEditorView::~PythonEditorView()
{
}
/**
* Runs the action specified by \a pMsg.
*/
bool PythonEditorView::onMsg(const char* pMsg,const char** ppReturn)
{
if (strcmp(pMsg,"Run")==0) {
executeScript();
return true;
}
else if (strcmp(pMsg,"StartDebug")==0) {
QTimer::singleShot(300, this, SLOT(startDebug()));
return true;
}
else if (strcmp(pMsg,"ToggleBreakpoint")==0) {
toggleBreakpoint();
return true;
}
return EditorView::onMsg(pMsg, ppReturn);
}
/**
* Checks if the action \a pMsg is available. This is for enabling/disabling
* the corresponding buttons or menu items for this action.
*/
bool PythonEditorView::onHasMsg(const char* pMsg) const
{
if (strcmp(pMsg,"Run")==0)
return true;
if (strcmp(pMsg,"StartDebug")==0)
return true;
if (strcmp(pMsg,"ToggleBreakpoint")==0)
return true;
return EditorView::onHasMsg(pMsg);
}
/**
* Runs the opened script in the macro manager.
*/
void PythonEditorView::executeScript()
{
// always save the macro when it is modified
if (EditorView::onHasMsg("Save"))
EditorView::onMsg("Save", nullptr);
try {
Application::Instance->macroManager()->run(Gui::MacroManager::File,fileName().toUtf8());
}
catch (const Base::SystemExitException&) {
// handle SystemExit exceptions
Base::PyGILStateLocker locker;
Base::PyException e;
e.ReportException();
}
}
void PythonEditorView::startDebug()
{
_pye->startDebug();
}
void PythonEditorView::toggleBreakpoint()
{
_pye->toggleBreakpoint();
}
void PythonEditorView::showDebugMarker(int line)
{
_pye->showDebugMarker(line);
}
void PythonEditorView::hideDebugMarker()
{
_pye->hideDebugMarker();
}
// ----------------------------------------------------------------------------
SearchBar::SearchBar(QWidget* parent)
: QWidget(parent)
, textEditor(nullptr)
{
horizontalLayout = new QHBoxLayout(this);
horizontalLayout->setSpacing(3);
closeButton = new QToolButton(this);
closeButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
closeButton->setAutoRaise(true);
connect(closeButton, &QToolButton::clicked, this, &SearchBar::deactivate);
horizontalLayout->addWidget(closeButton);
searchText = new QLineEdit(this);
searchText->setClearButtonEnabled(true);
horizontalLayout->addWidget(searchText);
connect(searchText, &QLineEdit::returnPressed, this, &SearchBar::findNext);
connect(searchText, &QLineEdit::textChanged, this, &SearchBar::findCurrent);
connect(searchText, &QLineEdit::textChanged, this, &SearchBar::updateButtons);
prevButton = new QToolButton(this);
prevButton->setIcon(style()->standardIcon(QStyle::SP_ArrowBack));
prevButton->setAutoRaise(true);
prevButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
horizontalLayout->addWidget(prevButton);
connect(prevButton, &QToolButton::clicked, this, &SearchBar::findPrevious);
nextButton = new QToolButton(this);
nextButton->setIcon(style()->standardIcon(QStyle::SP_ArrowForward));
nextButton->setAutoRaise(true);
nextButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
horizontalLayout->addWidget(nextButton);
connect(nextButton, &QToolButton::clicked, this, &SearchBar::findNext);
matchCase = new QCheckBox(this);
horizontalLayout->addWidget(matchCase);
connect(matchCase, &QCheckBox::toggled, this, &SearchBar::findCurrent);
matchWord = new QCheckBox(this);
horizontalLayout->addWidget(matchWord);
connect(matchWord, &QCheckBox::toggled, this, &SearchBar::findCurrent);
horizontalSpacer = new QSpacerItem(192, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer);
retranslateUi();
setMinimumWidth(minimumSizeHint().width());
updateButtons();
hide();
}
void SearchBar::setEditor(QPlainTextEdit* textEdit)
{
textEditor = textEdit;
}
void SearchBar::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Escape) {
hide();
return;
}
QWidget::keyPressEvent(event);
}
void SearchBar::retranslateUi()
{
prevButton->setText(tr("Previous"));
nextButton->setText(tr("Next"));
matchCase->setText(tr("Case sensitive"));
matchWord->setText(tr("Whole words"));
}
void SearchBar::activate()
{
show();
searchText->selectAll();
searchText->setFocus(Qt::ShortcutFocusReason);
}
void SearchBar::deactivate()
{
if (textEditor)
textEditor->setFocus();
hide();
}
void SearchBar::findPrevious()
{
findText(true, false, searchText->text());
}
void SearchBar::findNext()
{
findText(true, true, searchText->text());
}
void SearchBar::findCurrent()
{
findText(false, true, searchText->text());
}
void SearchBar::findText(bool skip, bool next, const QString& str)
{
if (!textEditor)
return;
QTextCursor cursor = textEditor->textCursor();
QTextDocument *doc = textEditor->document();
if (!doc || cursor.isNull())
return;
if (cursor.hasSelection())
cursor.setPosition((skip && next) ? cursor.position() : cursor.anchor());
bool found = true;
QTextCursor newCursor = cursor;
if (!str.isEmpty()) {
QTextDocument::FindFlags options;
if (!next)
options |= QTextDocument::FindBackward;
if (matchCase->isChecked())
options |= QTextDocument::FindCaseSensitively;
if (matchWord->isChecked())
options |= QTextDocument::FindWholeWords;
newCursor = doc->find(str, cursor, options);
if (newCursor.isNull()) {
QTextCursor ac(doc);
ac.movePosition(options & QTextDocument::FindBackward ? QTextCursor::End : QTextCursor::Start);
newCursor = doc->find(str, ac, options);
if (newCursor.isNull()) {
found = false;
newCursor = cursor;
}
}
}
if (!isVisible())
show();
textEditor->setTextCursor(newCursor);
QString styleSheet;
if (!found) {
styleSheet = QString::fromLatin1(
" QLineEdit {\n"
" background-color: rgb(221,144,161);\n"
" }\n"
);
}
searchText->setStyleSheet(styleSheet);
}
void SearchBar::updateButtons()
{
bool empty = searchText->text().isEmpty();
prevButton->setDisabled(empty);
nextButton->setDisabled(empty);
}
void SearchBar::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange) {
retranslateUi();
}
QWidget::changeEvent(event);
}
#include "moc_EditorView.cpp"