"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/findDialog.cpp" (12 Feb 2021, 5671 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "findDialog.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  *   findDialog was originally adapted from Qt Designer
    7  *   Copyright (C) 2001 Trolltech AS.  All rights reserved.
    8  ***************************************************************************/
    9 #include "findDialog.h"
   10 
   11 #include <QAction>
   12 #include <QPlainTextEdit>
   13 #include <QPushButton>
   14 #include <QScrollBar>
   15 #include <QTextEdit>
   16 
   17 
   18 findDialog::findDialog(QWidget* parentw) : QDialog(parentw)
   19 {
   20     ui.setupUi(this);
   21     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
   22     resize(size().width(), 1);
   23     ui.buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Find"));
   24     _editor = 0;
   25 
   26     QAction* act = new QAction(this);
   27     act->setSeparator(true);
   28     addAction(act);
   29     findAction = new QAction(this);
   30     addAction(findAction);
   31     findAction->setShortcut(QKeySequence::Find);
   32     findAction->setText(tr("Find"));
   33     findAction->setStatusTip(tr("Find in text"));
   34     connect(findAction, SIGNAL(triggered()), this, SLOT(findText()));
   35 
   36     findNextAction = new QAction(this);
   37     addAction(findNextAction);
   38     findNextAction->setShortcut(QKeySequence::FindNext);
   39     findNextAction->setText(tr("Find Next"));
   40     findNextAction->setStatusTip(tr("Find next occurrence"));
   41     connect(findNextAction, SIGNAL(triggered()), this, SLOT(findNext()));
   42 
   43     findPreviousAction = new QAction(this);
   44     addAction(findPreviousAction);
   45     findPreviousAction->setShortcut(QKeySequence::FindPrevious);
   46     findPreviousAction->setText(tr("Find Previous"));
   47     findPreviousAction->setStatusTip(tr("Find previous occurrence"));
   48     connect(findPreviousAction, SIGNAL(triggered()), this, SLOT(findPrevious()));
   49 }
   50 
   51 findDialog::~findDialog() {}
   52 
   53 
   54 void findDialog::init()
   55 {
   56     ui.notFound->hide();
   57     raise();
   58     if (_editor->inherits("QPlainTextEdit"))
   59         ui.comboFind->setEditText(qobject_cast<QPlainTextEdit*>(_editor)->textCursor().selectedText().trimmed());
   60     else if (_editor->inherits("QTextEdit"))
   61         ui.comboFind->setEditText(qobject_cast<QTextEdit*>(_editor)->textCursor().selectedText().trimmed());
   62     ui.comboFind->setFocus();
   63 }
   64 
   65 void findDialog::setEditor(QPlainTextEdit* editor)
   66 {
   67     _editor = editor;
   68 }
   69 
   70 void findDialog::setEditor(QTextEdit* editor)
   71 {
   72     _editor = editor;
   73 }
   74 
   75 int findDialog::exec()
   76 {
   77     if (!_editor)
   78         return (-1);
   79     init();
   80     return QDialog::exec();
   81 }
   82 
   83 void findDialog::show()
   84 {
   85     if (!_editor)
   86         return;
   87     init();
   88     QDialog::show();
   89 }
   90 
   91 void findDialog::accept()
   92 {
   93     if (ui.comboFind->currentText().isEmpty())
   94         return;
   95     if (_editor)
   96     {
   97         if (findText(ui.comboFind->currentText(), ui.radioForward->isChecked(), ui.checkCase->isChecked(),
   98                      ui.checkWords->isChecked()))
   99             QDialog::accept();
  100         else
  101             ui.notFound->show();
  102     }
  103 }
  104 
  105 void findDialog::findText()
  106 {
  107     show();
  108 }
  109 
  110 bool findDialog::findText(const QString& pattern, bool do_forward, bool do_case_sensitive, bool do_whole_words)
  111 {
  112     if (pattern.isEmpty())
  113         return false;
  114     m_find_pattern = pattern;
  115     m_find_case = do_case_sensitive;
  116     m_find_words = do_whole_words;
  117 
  118     QTextDocument* doc;
  119     QTextCursor c;
  120     if (_editor->inherits("QPlainTextEdit"))
  121     {
  122         QPlainTextEdit* ed = qobject_cast<QPlainTextEdit*>(_editor);
  123         doc = ed->document();
  124         c = ed->textCursor();
  125     }
  126     else if (_editor->inherits("QTextEdit"))
  127     {
  128         QTextEdit* ed = qobject_cast<QTextEdit*>(_editor);
  129         doc = ed->document();
  130         c = ed->textCursor();
  131     }
  132     else
  133         return false;
  134     QTextDocument::FindFlags options;
  135 
  136     if (c.hasSelection())
  137         c.setPosition(do_forward ? c.position() : c.anchor(), QTextCursor::MoveAnchor);
  138     if (!do_forward)
  139         options |= QTextDocument::FindBackward;
  140     if (m_find_case)
  141         options |= QTextDocument::FindCaseSensitively;
  142     if (m_find_words)
  143         options |= QTextDocument::FindWholeWords;
  144     QTextCursor newCursor(doc->find(m_find_pattern, c, options));
  145     bool found(true);
  146     if (newCursor.isNull())
  147     {
  148         QTextCursor ac(doc);
  149         ac.movePosition((options & QTextDocument::FindBackward) ? QTextCursor::End : QTextCursor::Start);
  150         newCursor = doc->find(m_find_pattern, ac, options);
  151         if (newCursor.isNull())
  152         {
  153             newCursor = c;
  154             found = false;
  155         }
  156     }
  157     if (!found)
  158         return false;
  159     if (_editor->inherits("QPlainTextEdit"))
  160     {
  161         QPlainTextEdit* ed = qobject_cast<QPlainTextEdit*>(_editor);
  162         ed->setTextCursor(newCursor);
  163         ed->verticalScrollBar()->setValue(ed->verticalScrollBar()->maximum());
  164         ed->centerCursor();
  165     }
  166     else if (_editor->inherits("QTextEdit"))
  167     {
  168         QTextEdit* ed = qobject_cast<QTextEdit*>(_editor);
  169         ed->setTextCursor(newCursor);
  170         ed->verticalScrollBar()->setValue(ed->verticalScrollBar()->maximum());
  171         ed->ensureCursorVisible();
  172         ed->verticalScrollBar()->setValue(ed->verticalScrollBar()->value() - ed->viewport()->height() / 3);
  173         ed->ensureCursorVisible();
  174     }
  175     return found;
  176 }
  177 
  178 void findDialog::findNext()
  179 {
  180     if (m_find_pattern.isEmpty())
  181         findText();
  182     else
  183         findText(m_find_pattern, true, m_find_case, m_find_words);
  184 }
  185 
  186 void findDialog::findPrevious()
  187 {
  188     if (m_find_pattern.isEmpty())
  189         findText();
  190     else
  191         findText(m_find_pattern, false, m_find_case, m_find_words);
  192 }