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 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
|
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "qsearchreplacepanel.h"
#include "flowlayout.h"
/*!
\file qsearchreplacepanel.cpp
\brief Implementation of the QSearchReplacePanel class.
\see QSearchReplacePanel
*/
#include "qeditor.h"
#include "qdocument.h"
#include "qdocumentline.h"
#include "qdocumentcursor.h"
#include "qdocumentsearch.h"
#include "qformatscheme.h"
#include "utilsSystem.h"
#include "configmanagerinterface.h"
#include "QCompleter"
/*!
\ingroup widgets
@{
*/
/*!
\class QSearchReplacePanel
\brief A panel that provide inline search/replace functionalities
*/
QCE_AUTO_REGISTER(QSearchReplacePanel)
bool cbHasFocus(const QComboBox* cb) {
if (cb->hasFocus()) return true;
if (cb->lineEdit() && cb->lineEdit()->hasFocus()) return true;
//test for popupped combobox
foreach (QObject* o, cb->children()) {
QWidget* w = qobject_cast<QWidget*>(o);
if (!w) continue;
if (w->isVisible() && w->hasFocus()) return true;
foreach (QObject* p, o->children()) {
QWidget* x = qobject_cast<QWidget*>(p);
if (!x) continue;
if (x->isVisible() && x->hasFocus()) return true;
}
}
return false;
}
QStringList findHistory, replaceHistory;
/*!
\brief Constructor
*/
QSearchReplacePanel::QSearchReplacePanel(QWidget *p)
: QPanel(p),m_search(0),m_lastDirection(false),useLineForSearch(false),searchOnlyInSelection(false)
{
setObjectName("searchPanel");
ConfigManagerInterface* conf = ConfigManagerInterface::getInstance();
//setupUi(this);
// do it completely programatic
//this->resize(801, 21);
QVBoxLayout *vboxLayout=new QVBoxLayout(this);
QWidget *searchWidget=new QWidget(0);
//searchWidget->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::MinimumExpanding);
vboxLayout->addWidget(searchWidget);
FlowLayoutX *flowLayout=new FlowLayoutX(searchWidget,1,1,1);
QSize buttonSize(22,22);
// find section
bClose = new QToolButton(this);
bClose->setObjectName(("bClose"));
bClose->setMinimumSize(buttonSize);
bClose->setMaximumSize(buttonSize);
bClose->setIcon(getRealIconCached("document-close"));
flowLayout->addWidget(bClose);
QLabel* lbFind = new QLabel(this);
lbFind->setObjectName(("lbFind"));
lbFind->setMinimumHeight(buttonSize.height());
flowLayout->addWidget(lbFind);
cFind = new QComboBox(this);
cFind->setEditable(true);
cFind->completer()->setCompletionMode(QCompleter::PopupCompletion);
cFind->completer()->setCaseSensitivity(Qt::CaseSensitive);
cFind->setObjectName(("cFind"));
QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Fixed);
sizePolicy4.setHorizontalStretch(2);
cFind->setSizePolicy(sizePolicy4);
cFind->setMinimumSize(QSize(120, 22));
conf->registerOption("Search/Find History", &findHistory, QStringList());
conf->linkOptionToObject(&findHistory, cFind, LinkOptions(LO_UPDATE_ALL | LO_DIRECT_OVERRIDE));
flowLayout->addWidget(cFind);
bNext = new QToolButton(this);
bNext->setObjectName(("bNext"));
bNext->setMinimumSize(buttonSize);
bNext->setMaximumSize(buttonSize);
bNext->setIcon(getRealIconCached("down"));
flowLayout->addWidget(bNext);
bPrevious = new QToolButton(this);
bPrevious->setObjectName(("bPrevious"));
bPrevious->setMinimumSize(buttonSize);
bPrevious->setMaximumSize(buttonSize);
bPrevious->setIcon(getRealIconCached("up"));
flowLayout->addWidget(bPrevious);
bCount = new QToolButton(this);
bCount->setObjectName(("bCount"));
bCount->setMinimumSize(buttonSize);
bCount->setMaximumSize(buttonSize);
bCount->setIcon(getRealIconCached("count"));
flowLayout->addWidget(bCount);
//cbCase = new QCheckBox();
cbCase = new QToolButton(this);
cbCase->setCheckable(true);
cbCase->setObjectName(("cbCase"));
cbCase->setToolTip(tr("Enables case sensitive search."));
cbCase->setMinimumSize(buttonSize);
cbCase->setMaximumSize(buttonSize);
cbCase->setIcon(getRealIconCached("case"));
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, caseConfig, true, "Search/Case Sensitive", cbCase);
flowLayout->addWidget(cbCase);
cbWords = new QToolButton(this);
cbWords->setCheckable(true);
cbWords->setToolTip(tr("Only searches for whole words."));
cbWords->setObjectName(("cbWords"));
cbWords->setMinimumSize(buttonSize);
cbWords->setMaximumSize(buttonSize);
cbWords->setIcon(getRealIconCached("word"));
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, wordConfig, false, "Search/Whole Words", cbWords);
flowLayout->addWidget(cbWords);
cbRegExp = new QToolButton(this);
cbRegExp->setCheckable(true);
cbRegExp->setToolTip(tr("This interprets the search text as a regular expression.\nSome common regexps:\n r* will find any amount of r, r+ is equal to rr*, a? will matches a or nothing,\n () groups expressions together, [xyz] will find x,y, or z, . matches everything, \\. matches .\nYou can use \\1 to \\9 in the replace text to insert a submatch."));
cbRegExp->setObjectName(("cbRegExp"));
cbRegExp->setMinimumSize(buttonSize);
cbRegExp->setMaximumSize(buttonSize);
cbRegExp->setIcon(getRealIconCached("regex"));
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, regexConfig, false, "Search/Regular Expression", cbRegExp);
flowLayout->addWidget(cbRegExp);
cbHighlight = new QToolButton(this);
cbHighlight->setCheckable(true);
cbHighlight->setObjectName(("cbHighlight"));
cbHighlight->setToolTip(tr("Highlights search matches and replaced text."));
cbHighlight->setIcon(getRealIconCached("highlight"));
cbHighlight->setMinimumSize(buttonSize);
cbHighlight->setMaximumSize(buttonSize);
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, highlightConfig, true, "Search/Highlight", cbHighlight);
flowLayout->addWidget(cbHighlight);
cbCursor = new QToolButton(this);
cbCursor->setCheckable(true);
cbCursor->setToolTip(tr("Starts the search from the current cursor position."));
cbCursor->setObjectName(("cbCursor"));
cbCursor->setMinimumSize(buttonSize);
cbCursor->setMaximumSize(buttonSize);
cbCursor->setIcon(getRealIconCached("cursor"));
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, cursorConfig, true, "Search/Cursor", cbCursor);
flowLayout->addWidget(cbCursor);
cbSelection = new QToolButton(this);
cbSelection->setCheckable(true);
cbSelection->setToolTip(tr("Only searches in the selected text."));
cbSelection->setObjectName(("cbSelection"));
cbSelection->setMinimumSize(buttonSize);
cbSelection->setMaximumSize(buttonSize);
cbSelection->setIcon(getRealIconCached("selection"));
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, selectionConfig, false, "Search/Selection", cbSelection);
flowLayout->addWidget(cbSelection);
bExtend = new QToolButton(this);
bExtend->setToolTip(tr("Extended Search"));
bExtend->setObjectName(("bExtend"));
bExtend->setMinimumSize(buttonSize);
bExtend->setMaximumSize(buttonSize);
bExtend->setIcon(getRealIconCached("extend"));
flowLayout->addWidget(bExtend);
connect(bExtend, SIGNAL(clicked()), this, SIGNAL(showExtendedSearch()));
// replace section
QWidget *replaceWidget=new QWidget(0);
vboxLayout->addWidget(replaceWidget);
FlowLayoutX *flowLayout2=new FlowLayoutX(replaceWidget,1,1,1);
cbReplace = new QCheckBox(this);
cbReplace->setObjectName(("cbReplace"));
cbReplace->setChecked(true);
cbReplace->setMinimumSize(buttonSize);
cbReplace->setMaximumSize(buttonSize);
flowLayout2->addWidget(cbReplace);
QLabel *lbReplace = new QLabel(this);
lbReplace->setObjectName("lbReplace");
lbReplace->setMinimumHeight(buttonSize.height());
flowLayout2->addWidget(lbReplace);
cReplace = new QComboBox(this);
cReplace->setEditable(true);
cReplace->completer()->setCompletionMode(QCompleter::PopupCompletion);
cReplace->completer()->setCaseSensitivity(Qt::CaseSensitive);
cReplace->setObjectName(("cReplace"));
cReplace->setEnabled(true);
QSizePolicy sizePolicy7(QSizePolicy::Minimum, QSizePolicy::Fixed);
sizePolicy7.setHorizontalStretch(2);
sizePolicy7.setVerticalStretch(0);
sizePolicy7.setHeightForWidth(cReplace->sizePolicy().hasHeightForWidth());
cReplace->setSizePolicy(sizePolicy4);
cReplace->setMinimumSize(QSize(120, 22));
// cReplace->setMaximumSize(QSize(1200, 16777215));
conf->registerOption("Search/Replace History", &replaceHistory, QStringList());
conf->linkOptionToObject(&replaceHistory, cReplace, LinkOptions(LO_UPDATE_ALL | LO_DIRECT_OVERRIDE));
flowLayout2->addWidget(cReplace);
bReplaceNext = new QToolButton(this);
bReplaceNext->setObjectName(("bReplaceNext"));
bReplaceNext->setMinimumSize(buttonSize);
bReplaceNext->setMaximumSize(buttonSize);
bReplaceNext->setIcon(getRealIconCached("replacedown"));
flowLayout2->addWidget(bReplaceNext);
bReplacePrevious = new QToolButton(this);
bReplacePrevious->setObjectName(("bReplacePrevious"));
bReplacePrevious->setMinimumSize(buttonSize);
bReplacePrevious->setMaximumSize(buttonSize);
bReplacePrevious->setIcon(getRealIconCached("replaceup"));
flowLayout2->addWidget(bReplacePrevious);
bReplaceAll = new QToolButton(this);
bReplaceAll->setObjectName(("bReplaceAll"));
bReplaceAll->setMinimumSize(buttonSize);
bReplaceAll->setMaximumSize(buttonSize);
bReplaceAll->setIcon(getRealIconCached("replaceall"));
flowLayout2->addWidget(bReplaceAll);
cbPrompt = new QToolButton(this);
cbPrompt->setCheckable(true);
cbPrompt->setToolTip(tr("Ask before any match is replaced."));
cbPrompt->setObjectName(("cbPrompt"));
cbPrompt->setMinimumSize(buttonSize);
cbPrompt->setMaximumSize(buttonSize);
cbPrompt->setIcon(getRealIconCached("prompt"));
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, askConfig, false, "Search/Ask before Replace", cbPrompt);
flowLayout2->addWidget(cbPrompt);
cbEscapeSeq = new QToolButton(this);
cbEscapeSeq->setCheckable(true);
cbEscapeSeq->setToolTip(tr("Enables the use of escape characters. These are:\n \\n = new line, \\r = carriage return, \\t = tab, \\\\ = \\"));
cbEscapeSeq->setObjectName(("cbEscapeSeq"));
cbEscapeSeq->setMinimumSize(buttonSize);
cbEscapeSeq->setMaximumSize(buttonSize);
cbEscapeSeq->setIcon(getRealIconCached("escape"));
CONFIG_DECLARE_OPTION_WITH_OBJECT(conf, bool, escapeConfig, false, "Search/Escape Sequence", cbEscapeSeq);
flowLayout2->addWidget(cbEscapeSeq);
lReplacementText = new QLabel(this);
flowLayout2->addWidget(lReplacementText);
//retranslateUi(this);
QObject::connect(cbReplace, SIGNAL(toggled(bool)), replaceWidget, SLOT(setVisible(bool)));
QObject::connect(bClose, SIGNAL(clicked()), this, SLOT(close()));
// connect by name
QMetaObject::connectSlotsByName(this);
connect(cFind->lineEdit(),SIGNAL(textEdited(QString)),SLOT(cFind_textEdited(QString)));
connect(cReplace->lineEdit(),SIGNAL(textEdited(QString)),SLOT(cReplace_textEdited(QString)));
// set texts
bClose->setToolTip(tr("Close search/replace panel"));
cFind->setToolTip(tr("Text or pattern to search for"));
bNext->setToolTip(tr("Find next"));
bPrevious->setToolTip(tr("Find previous"));
bCount->setToolTip(tr("Count occurences"));
cReplace->setToolTip(tr("Replacement text"));
bReplaceNext->setToolTip(tr("Replace and find next"));
bReplacePrevious->setToolTip(tr("Replace and find previous"));
bReplaceAll->setToolTip(tr("Replace all"));
lbFind->setText(tr("Find:"));
lbReplace->setText(tr("Replace:"));
int wd=qMax(lbFind->sizeHint().width(),lbReplace->sizeHint().width());
lbFind->setMinimumWidth(wd);
lbReplace->setMinimumWidth(wd);
lbFind->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
lbReplace->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
cFind->installEventFilter(this);
cReplace->installEventFilter(this);
Q_ASSERT(cFind->completer()->popup());
cFind->completer()->popup()->installEventFilter(this);
cReplace->completer()->popup()->installEventFilter(this);
}
/*!
\brief Empty destructor
*/
QSearchReplacePanel::~QSearchReplacePanel()
{
if ( m_search )
delete m_search;
m_search=0;
}
/*!
*/
QString QSearchReplacePanel::type() const
{
return "Search";
}
bool QSearchReplacePanel::isReplaceModeActive() const{
return cbReplace->isChecked();
}
QDocumentCursor QSearchReplacePanel::getSearchScope() const{
return m_search->scope();
}
void QSearchReplacePanel::setUseLineForSearch(bool b){
useLineForSearch = b;
}
bool QSearchReplacePanel::getUseLineForSearch() const{
return useLineForSearch;
}
void QSearchReplacePanel::setSearchOnlyInSelection(bool b){
searchOnlyInSelection = b;
}
bool QSearchReplacePanel::getSearchOnlyInSelection() const{
return searchOnlyInSelection;
}
/*!
\brief
*/
void QSearchReplacePanel::editorChange(QEditor *e)
{
if ( editor() )
{
disconnect( editor(), SIGNAL( cursorPositionChanged() ),
this , SLOT ( cursorPositionChanged() ) );
}
if ( e )
{
connect(e , SIGNAL( cursorPositionChanged() ),
this, SLOT ( cursorPositionChanged() ) );
}
}
bool QSearchReplacePanel::forward(QMouseEvent *e)
{
Q_UNUSED(e)
/*
This panel does not need mouse events to be forwarded to the editor.
Even more, it requires them not to be forwarded...
*/
return false;
}
/*!
*/
void QSearchReplacePanel::display(int mode, bool replace)
{
//qDebug("display(%i)", replace);
if (! m_search) init();
bool visible = true;
if ( mode < 0 )
visible = (replace != cbReplace->isChecked()) || isHidden();
else
visible = mode;
if ( visible )
{
//frameReplace->setVisible(replace);
bool focusFindEdit = true;
if (m_search){
if(editor()->cursor().hasSelection()){
if(editor()->cursor().anchorLineNumber()!=editor()->cursor().lineNumber() || !useLineForSearch){
if (searchOnlyInSelection){
if(cbSelection->isChecked()) on_cbSelection_toggled(true);
else cbSelection->setChecked(true);
}
} if ( (cbHasFocus(cFind) || cbHasFocus(cReplace)) && visible) {
//don't copy selection to cFind, if the panel is in use
if ( cbHasFocus(cFind) && replace )
focusFindEdit = false; //switch to replace edit, if the cursor is in the find edit (sideeffect: ctrl+r toggles active edit)
} else {
// single line selection
// copy content to cFind (doesn't trigger textEdited; don't call textEdited to prevent cursor jumping)
cbSelection->setChecked(false);
cFind->setEditText(editor()->cursor().selectedText());
m_search->setSearchText(cFind->currentText());
}
} else if (useLineForSearch && !replace && !((cbHasFocus(cFind) || cbHasFocus(cReplace)) && visible)) {
// use word under cursor if no selection is present (qt creator behavior)
QDocumentCursor m_cursor=editor()->cursor();
m_cursor.select(QDocumentCursor::WordUnderCursor);
if (!m_cursor.selectedText().isEmpty()) {
cFind->setEditText(m_cursor.selectedText());
m_search->setSearchText(cFind->currentText());
}
} else if ( cbHasFocus(cFind) && replace && visible)
focusFindEdit = false;
if (cbHighlight->isChecked() && !m_search->hasOption(QDocumentSearch::HighlightAll))
m_search->setOption(QDocumentSearch::HighlightAll, true);
}
cbReplace->setChecked(replace);
if (focusFindEdit) {
cFind->setFocus();
cFind->lineEdit()->selectAll();
} else {
cReplace->setFocus();
cReplace->lineEdit()->selectAll();
}
//show();
}else closeEvent(0);
setVisible(visible);
if ( !visible ){
editor()->setFocus();
bExtend->setChecked(false);
}
}
void QSearchReplacePanel::closeSomething(bool closeTogether){
/*qDebug() << "popup"<< QApplication::activePopupWidget();
foreach (QObject* o, cFind->children()) {
qDebug()<<"child"<<o;
if (qobject_cast<QWidget*>(o)) qDebug() << " "<<(qobject_cast<QWidget*>(o))->hasFocus()<<(qobject_cast<QWidget*>(o))->isVisible();
foreach (QObject* p, o->children()) {
qDebug()<<"child child "<<p;
if (qobject_cast<QWidget*>(p))
qDebug() << " "<<(qobject_cast<QWidget*>(p))->hasFocus()
<<(qobject_cast<QWidget*>(p))->isVisible();
foreach (QObject* q, p->children()) {
qDebug()<<"child child child "<<q;
if (qobject_cast<QWidget*>(q)) qDebug() << " "<<(qobject_cast<QWidget*>(q))->hasFocus()<<(qobject_cast<QWidget*>(q))->isVisible();
}
}
}*/
if (cFind->completer()->popup()->isVisible() && cFind->completer()->popup()->hasFocus())
cFind->setFocus();
else if (cReplace->completer()->popup()->isVisible() && cReplace->completer()->popup()->hasFocus())
cReplace->setFocus();
else if (isReplaceModeActive() & !closeTogether)
display(1,false);
else
display(0,false);
}
void QSearchReplacePanel::findNext(){
findReplace(m_lastDirection);
}
void QSearchReplacePanel::updateSearchOptions(bool replace, bool replaceAll){
if ( !m_search ) init();
if ( cbCursor->isChecked() && !m_search->cursor().isValid() )
m_search->setCursor(editor()->cursor()); //start from current cursor if no known cursor
if ( !cbCursor->isChecked() && replaceAll )
m_search->setCursor(QDocumentCursor());
if ( m_search->searchText()!=cFind->currentText() )
m_search->setSearchText(cFind->currentText());
if ( replace && m_search->replaceText()!=cReplace->currentText() )
m_search->setReplaceText(cReplace->currentText());
m_search->setOption(QDocumentSearch::Replace,replace);
}
QStringList QSearchReplacePanel::getHistory(bool rfindHistory){
return (rfindHistory?findHistory:replaceHistory);
}
void QSearchReplacePanel::setHistory(const QStringList& newHistory, bool sfindHistory){
QStringList& history = (sfindHistory?findHistory:replaceHistory);
history = newHistory;
ConfigManagerInterface::getInstance()->updateAllLinkedObjects(&history);
}
void QSearchReplacePanel::rememberLastSearch(QStringList& history, const QString& str, bool incremental){
if (history.isEmpty()) {
history.append(str);
ConfigManagerInterface::getInstance()->updateAllLinkedObjects(&history);
return;
}
if (!incremental && !history.contains(str)) {
history.append(str);
ConfigManagerInterface::getInstance()->updateAllLinkedObjects(&history);
}
}
void QSearchReplacePanel::findReplace(bool backward, bool replace, bool replaceAll, bool countOnly)
{
Q_ASSERT(!(replace && countOnly));
Q_ASSERT(!(replaceAll && !replace));
if ( !m_search && !isVisible() ) {
display(1, false);
return;
}
updateSearchOptions(replace,replaceAll);;
m_lastDirection=backward;
if (!countOnly) m_search->next(backward, replaceAll, !cbPrompt->isChecked(), true);
else {
m_search->setOption(QDocumentSearch::Silent, true);
QDocumentCursor startCur = m_search->cursor();
int question = QMessageBox::Yes;
if (startCur.isValid() && cbCursor->isChecked() &&
((startCur.selectionStart() > m_search->scope().selectionStart() && startCur.selectionEnd() < m_search->scope().selectionEnd()) || !m_search->scope().isValid())) {
m_search->setCursor(m_search->cursor().selectionEnd());
int count = m_search->next(false, true, true, false);
question = QMessageBox::information(this,tr("Count result"),tr("The search text occurs %1 times after the current cursor. Do you want to restart from the beginning of the scope?").arg(count),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}
if (question == QMessageBox::Yes) {
m_search->setCursor(QDocumentCursor());
int count = m_search->next(false, true, true, false);
QMessageBox::information(this,tr("Count result"),tr("The search text occurs %1 times within the search scope.").arg(count),QMessageBox::Ok);
}
m_search->setCursor(startCur);
m_search->setOption(QDocumentSearch::Silent, false);
}
rememberLastSearch(findHistory,cFind->currentText(),m_search->options() & QDocumentSearch::Silent);
rememberLastSearch(replaceHistory,cReplace->currentText(),m_search->options() & QDocumentSearch::Silent);
if (isVisible() && !cbHasFocus(cFind) && !cbHasFocus(cReplace) ) {
if (replace) cReplace->setFocus();
else cFind->setFocus();
}
updateReplacementHint();
}
void QSearchReplacePanel::find(QString text, bool backward, bool highlight, bool regex, bool word, bool caseSensitive, bool fromCursor, bool selection){
cbSelection->setChecked(selection);
cbCursor->setChecked(fromCursor);
find(text, backward, highlight, regex, word, caseSensitive);
}
void QSearchReplacePanel::selectAllMatches(){
if (!editor()) return;
updateSearchOptions(false,false);
m_search->setOption(QDocumentSearch::Silent, true); //don't forget
QDocumentCursor startCur = m_search->cursor();
m_search->setCursor(QDocumentCursor());
editor()->setCursor(QDocumentCursor());
QDocumentCursor last = QDocumentCursor();
int count = 0;
while (m_search->next(false, false, false, false) && m_search->cursor().isValid()) {
if (!count) editor()->setCursor(m_search->cursor());
else if (last < m_search->cursor()) editor()->addCursorMirror(m_search->cursor());
else break;
last = m_search->cursor();
count++;
}
m_search->setCursor(startCur);
m_search->setOption(QDocumentSearch::Silent, false);
if (count) editor()->setFocus();
else if (!isVisible()) display(1,false);
}
void QSearchReplacePanel::find(QString text, bool backward, bool highlight, bool regex, bool word, bool caseSensitive){
if (!isVisible()) display(1,false);
if (m_search && m_search->searchText()!=text) {
delete m_search;
m_search=0;
}
//if (!m_search) editor()->setCursorPosition(0,0); ??
if(!m_search) init();
cFind->setEditText(text);
cbHighlight->setChecked(highlight);
cbRegExp->setChecked(regex);
cbCase->setChecked(caseSensitive);
cbWords->setChecked(word);
findReplace(backward);
}
void QSearchReplacePanel::setOptions(int searchOptions, bool cursor, bool selection){
cbRegExp->setChecked(searchOptions & QDocumentSearch::RegExp);
cbCase->setChecked(searchOptions &QDocumentSearch::CaseSensitive);
cbWords->setChecked(searchOptions &QDocumentSearch::WholeWords);
cbHighlight->setChecked(searchOptions &QDocumentSearch::HighlightAll);
cbReplace->setChecked(searchOptions & QDocumentSearch::Replace);
cbPrompt->setChecked(searchOptions & QDocumentSearch::Prompt);
cbEscapeSeq->setChecked(searchOptions & QDocumentSearch::EscapeSeq);
cbCursor->setChecked(cursor);
cbSelection->setChecked(selection);
}
/*!
*/
void QSearchReplacePanel::hideEvent(QHideEvent *)
{
}
void QSearchReplacePanel::closeEvent(QCloseEvent *)
{
//beware: the CloseEvent could be 0
if ( m_search )
{
if (isVisible() && editor() && m_search->lastReplacedPosition().isValid()) {
editor()->setCursor(m_search->lastReplacedPosition(), false);
editor()->ensureCursorVisible(QEditor::Navigation);
}
//m_search->highlightSelection(false);
m_search->setOption(QDocumentSearch::HighlightAll, false);
//reset search scope
m_search->setScope(QDocumentCursor());
if (!cbCursor->isChecked())
m_search->setCursor(QDocumentCursor());
emit onClose();
//delete m_search;
//m_search=0;
}
}
bool QSearchReplacePanel::eventFilter(QObject *o, QEvent *e)
{
// if (e->type() == QEvent::KeyPress)
// qDebug() << QDateTime::currentDateTime() << "eventfilter:" << o << (o?o->objectName():"..") << " vs. " << cFind->completer()->popup();
if ( o == cFind || o == cReplace )
{
int kc;
switch ( e->type() )
{
/*
case QEvent::FocusIn :
cFind->grabKeyboard();
break;*/
/* case QEvent::FocusOut :
e->setAccepted(true);
return true;
//cFind->releaseKeyboard();
//break;
*/
case QEvent::KeyPress :
kc = static_cast<QKeyEvent*>(e)->key();
if ( ( (kc == Qt::Key_Enter) || (kc == Qt::Key_Return) ) )
{
//on_cFind_returnPressed();
if (cbHasFocus(cReplace))
on_cReplace_returnPressed(Qt::ShiftModifier & static_cast<QKeyEvent*>(e)->modifiers());
else
on_cFind_returnPressed(Qt::ShiftModifier & static_cast<QKeyEvent*>(e)->modifiers());
return true;
} else if ( kc == Qt::Key_Escape) {
if ( cbReplace->isChecked() )
display(0,false);
else
display(0,false);
return true;
} else if ( kc == Qt::Key_Tab || kc == Qt::Key_Backtab ) {
if ( cbReplace->isChecked() )
{
if ( cbHasFocus(cFind) ) {
cReplace->setFocus();
cReplace->lineEdit()->selectAll();
} else {
cFind->setFocus();
cFind->lineEdit()->selectAll();
}
}
return true;
}
break;
default:
break;
}
} else if ( o == cFind->completer()->popup() || o == cReplace->completer()->popup() ) {
int kc;
switch ( e->type() )
{
case QEvent::KeyPress :
kc = static_cast<QKeyEvent*>(e)->key();
if ( (kc == Qt::Key_Enter) || (kc == Qt::Key_Return) )
{
//on_cFind_returnPressed();
if (cbHasFocus(cReplace))
on_cReplace_returnPressed(Qt::ShiftModifier & static_cast<QKeyEvent*>(e)->modifiers());
else
on_cFind_returnPressed(Qt::ShiftModifier & static_cast<QKeyEvent*>(e)->modifiers());
return true;
} else if ( kc == Qt::Key_Tab || kc == Qt::Key_Backtab ) {
qobject_cast<QWidget*>(o)->hide();
if ( cbReplace->isChecked() )
{
if ( cbHasFocus(cFind) )
cReplace->setFocus();
else
cFind->setFocus();
}
return true;
} else if ( static_cast<QKeyEvent*>(e)->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) {
qobject_cast<QWidget*>(o)->hide(); //keep shortcuts working with active popup completer
}
break;
default:;
}
}
return QWidget::eventFilter(o, e);
}
void QSearchReplacePanel::cFind_textEdited(const QString& text)
{
if (! m_search) init();
m_search->setSearchText(text);
if ( text.isEmpty() )
{
cFind->lineEdit()->setStyleSheet(QString());
return;
}
QDocumentCursor cur = editor()->cursor();
if (m_search->cursor().isValid()){
cur = m_search->cursor();
m_search->setCursor(m_search->cursor().selectionStart());
}
m_search->setOption(QDocumentSearch::Silent,true);
findReplace(false);
m_search->setOption(QDocumentSearch::Silent,false);
if ( m_search->cursor().isNull() )
cFind->lineEdit()->setStyleSheet("QLineEdit { background: red; color : white; }");
else if ((m_search->cursor().anchorLineNumber() < cur.anchorLineNumber()) ||
(m_search->cursor().anchorLineNumber() == cur.anchorLineNumber() && m_search->cursor().anchorColumnNumber()<cur.anchorColumnNumber())) {
cFind->lineEdit()->setStyleSheet("QLineEdit { background: yellow; color : black; }");
editor()->setCursor(m_search->cursor());
} else {
cFind->lineEdit()->setStyleSheet(QString());
editor()->setCursor(m_search->cursor());
}
updateReplacementHint();
}
void QSearchReplacePanel::on_cFind_returnPressed(bool backward)
{
cFind->lineEdit()->setStyleSheet(QString());
findReplace(backward);
}
void QSearchReplacePanel::on_cReplace_returnPressed(bool backward){
cFind->lineEdit()->setStyleSheet(QString());
findReplace(backward,true);
}
void QSearchReplacePanel::cReplace_textEdited(const QString& text)
{
if ( m_search ) {
m_search->setReplaceText(text);
updateReplacementHint();
}
}
void QSearchReplacePanel::on_cbPrompt_toggled(bool on){
if ( m_search )
m_search->setOption(QDocumentSearch::Prompt, on);
if ( cFind->isVisible() )
cFind->setFocus();
}
void QSearchReplacePanel::on_cbReplace_toggled(bool on)
{
if ( m_search )
m_search->setOption(QDocumentSearch::Replace, on);
if ( cFind->isVisible() )
cFind->setFocus();
}
void QSearchReplacePanel::on_cbWords_toggled(bool on)
{
if ( m_search )
m_search->setOption(QDocumentSearch::WholeWords, on);
if (on && cbRegExp->isChecked())
cbRegExp->setChecked(false); //word and regexp is not possible
if ( cFind->isVisible() )
cFind->setFocus();
}
void QSearchReplacePanel::on_cbRegExp_toggled(bool on)
{
if ( m_search )
m_search->setOption(QDocumentSearch::RegExp, on);
if (on && cbWords->isChecked())
cbWords->setChecked(false); //word and regexp is not possible
if ( cFind->isVisible() )
cFind->setFocus();
updateReplacementHint();
}
void QSearchReplacePanel::on_cbCase_toggled(bool on)
{
if ( m_search )
m_search->setOption(QDocumentSearch::CaseSensitive, on);
if ( cFind->isVisible() )
cFind->setFocus();
}
void QSearchReplacePanel::on_cbCursor_toggled(bool on)
{
if ( m_search )
{
if ( on && !cbSelection->isChecked())
m_search->setCursor(editor()->cursor());
else
m_search->setCursor(QDocumentCursor());
}
if ( cFind->isVisible() )
cFind->setFocus();
}
void QSearchReplacePanel::on_cbHighlight_toggled(bool on)
{
if ( !m_search )
init();
if ( m_search )
{
m_search->setOption(QDocumentSearch::HighlightAll, on);
}
if ( cFind->isVisible() )
cFind->setFocus();
}
void QSearchReplacePanel::on_cbSelection_toggled(bool on)
{
if ( m_search ) {
m_search->setScope(on ? editor()->cursor() : QDocumentCursor());
if(on){
// deselect cursor to show search scope (which is below cuersor highlight)
QDocumentCursor cur=editor()->cursor();
if(cur.hasSelection()){
cur.clearSelection();
editor()->setCursor(cur);
}
}
}
cFind->setFocus();
}
void QSearchReplacePanel::on_cbEscapeSeq_toggled(bool on)
{
if ( m_search )
m_search->setOption(QDocumentSearch::EscapeSeq, on);
if ( cFind->isVisible() )
cFind->setFocus();
updateReplacementHint();
}
void QSearchReplacePanel::on_bNext_clicked()
{
cFind->lineEdit()->setStyleSheet(QString());
findReplace(false);
}
void QSearchReplacePanel::on_bPrevious_clicked()
{
cFind->lineEdit()->setStyleSheet(QString());
findReplace(true);
}
void QSearchReplacePanel::on_bCount_clicked()
{
cFind->lineEdit()->setStyleSheet(QString());
findReplace(false, false, false, true);
}
void QSearchReplacePanel::on_bReplaceNext_clicked()
{
cFind->lineEdit()->setStyleSheet(QString());
findReplace(false,true);
}
void QSearchReplacePanel::on_bReplacePrevious_clicked()
{
cFind->lineEdit()->setStyleSheet(QString());
findReplace(true,true);
}
void QSearchReplacePanel::on_bReplaceAll_clicked()
{
cFind->lineEdit()->setStyleSheet(QString());
findReplace(false,true,true);
}
void QSearchReplacePanel::init()
{
if ( m_search )
{
delete m_search;
m_search = 0;
}
QDocumentSearch::Options opt;
if ( cbRegExp->isChecked() )
opt |= QDocumentSearch::RegExp;
if ( cbCase->isChecked() )
opt |= QDocumentSearch::CaseSensitive;
if ( cbWords->isChecked() )
opt |= QDocumentSearch::WholeWords;
if ( cbHighlight->isChecked())
opt |= QDocumentSearch::HighlightAll;
if ( cbReplace->isChecked())
opt |= QDocumentSearch::Replace;
if ( cbPrompt->isChecked() )
opt |= QDocumentSearch::Prompt;
if ( cbEscapeSeq->isChecked() )
opt |= QDocumentSearch::EscapeSeq;
m_search = new QDocumentSearch( editor(),
cFind->currentText(),
opt,
cbReplace->isChecked()
?
cReplace->currentText()
:
QString()
);
if ( cbSelection->isChecked() && editor()->cursor().hasSelection()){
m_search->setScope(editor()->cursor());
} else if ( cbCursor->isChecked() )
m_search->setCursor(editor()->cursor());
}
void QSearchReplacePanel::cursorPositionChanged()
{
if ( m_search )
{
if ( editor()->cursor() == m_search->cursor() )
return;
if ( cbSelection->isChecked() && editor()->cursor().hasSelection() && isVisible()){
m_search->setScope(editor()->cursor());
} /*else {
if ( cbCursor->isChecked() )
m_search->setCursor(editor()->cursor());
}*/
if ( cbCursor->isChecked() )
m_search->setCursor(editor()->cursor());
}
}
void QSearchReplacePanel::updateReplacementHint(){
if (!m_search) return;
if (m_search->hasOption(QDocumentSearch::RegExp) || m_search->hasOption(QDocumentSearch::EscapeSeq))
lReplacementText->setText(m_search->replaceTextExpanded());
else
lReplacementText->setText("");
}
QString QSearchReplacePanel::getSearchText() const{
return cFind->currentText();
}
QString QSearchReplacePanel::getReplaceText() const
{
return cReplace->currentText();
}
bool QSearchReplacePanel::getSearchIsCase() const
{
return cbCase->isChecked();
}
bool QSearchReplacePanel::getSearchIsWords() const
{
return cbWords->isChecked();
}
bool QSearchReplacePanel::getSearchIsRegExp() const
{
return cbRegExp->isChecked();
}
/*! @} */
|