diff options
author | nils <[email protected]> | 2016-02-07 20:49:42 +0000 |
---|---|---|
committer | nils <[email protected]> | 2016-02-07 20:49:42 +0000 |
commit | 646ec2ba7cd74d65014fac420d58c6e402d6ef98 (patch) | |
tree | 53c173856129b8fc4fed337dec21d7437ffe2b58 | |
parent | seoma more refactoring (diff) | |
download | gpg4usb-646ec2ba7cd74d65014fac420d58c6e402d6ef98.tar.gz gpg4usb-646ec2ba7cd74d65014fac420d58c6e402d6ef98.zip |
added findwidget
-rw-r--r-- | findwidget.cpp | 123 | ||||
-rw-r--r-- | findwidget.h | 42 | ||||
-rw-r--r-- | gpg4usb.pro | 2 | ||||
-rw-r--r-- | gpg4usb.qrc | 2 | ||||
-rw-r--r-- | mainwindow.cpp | 22 | ||||
-rw-r--r-- | mainwindow.h | 6 | ||||
-rw-r--r-- | release/icons/button_next.png | bin | 0 -> 2583 bytes | |||
-rw-r--r-- | release/icons/button_previous.png | bin | 0 -> 5229 bytes | |||
-rw-r--r-- | textedit.h | 12 |
9 files changed, 203 insertions, 6 deletions
diff --git a/findwidget.cpp b/findwidget.cpp new file mode 100644 index 0000000..ecaa9dd --- /dev/null +++ b/findwidget.cpp @@ -0,0 +1,123 @@ + +#include "findwidget.h" + +FindWidget::FindWidget(QWidget *parent, QTextEdit *edit) : + QWidget(parent) +{ + mTextpage = edit; + findEdit = new QLineEdit(this); + QPushButton *closeButton= new QPushButton(this->style()->standardIcon(QStyle::SP_TitleBarCloseButton),"",this); + QPushButton *nextButton= new QPushButton(QIcon(":button_next.png"), ""); + QPushButton *previousButton= new QPushButton(QIcon(":button_previous.png"), ""); + + QHBoxLayout *notificationWidgetLayout = new QHBoxLayout(this); + notificationWidgetLayout->setContentsMargins(10,0,0,0); + notificationWidgetLayout->addWidget(new QLabel(tr("Find:"))); + notificationWidgetLayout->addWidget(findEdit,2); + notificationWidgetLayout->addWidget(nextButton); + notificationWidgetLayout->addWidget(previousButton); + notificationWidgetLayout->addWidget(closeButton); + + this->setLayout(notificationWidgetLayout); + connect(findEdit,SIGNAL(textEdited(QString)),this,SLOT(slotFind())); + connect(findEdit,SIGNAL(returnPressed()),this,SLOT(slotFindNext())); + connect(nextButton,SIGNAL(clicked()),this,SLOT(slotFindNext())); + connect(previousButton,SIGNAL(clicked()),this,SLOT(slotFindPrevious())); + connect(closeButton,SIGNAL(clicked()),this,SLOT(slotClose())); + + // The timer is necessary for setting the focus + QTimer::singleShot(0, findEdit, SLOT(setFocus())); +} + +void FindWidget::setBackground() +{ + QTextCursor cursor = mTextpage->textCursor(); + // if match is found set background of QLineEdit to white, otherwise to red + QPalette bgPalette( findEdit->palette() ); + if ((cursor.position() == -1) && (!findEdit->text().isEmpty())) { + bgPalette.setColor( QPalette::Base, "#ececba"); + } else { + bgPalette.setColor( QPalette::Base, Qt::white); + } + findEdit->setPalette(bgPalette); +} + +void FindWidget::slotFindNext() +{ + QTextCursor cursor = mTextpage->textCursor(); + cursor = mTextpage->document()->find(findEdit->text(), cursor, QTextDocument::FindCaseSensitively); + // if end of document is reached, restart search from beginning + if (cursor.position() == -1) { + //cursor.setPosition(0); + QTextCursor cursor = mTextpage->document()->find(findEdit->text(), QTextCursor::Start, QTextDocument::FindCaseSensitively); + } + + mTextpage->setTextCursor(cursor); + this->setBackground(); +} + +void FindWidget::slotFind() +{ + QTextCursor cursor = mTextpage->textCursor(); + + if (cursor.anchor() == -1) { + cursor = mTextpage->document()->find(findEdit->text(), cursor, QTextDocument::FindCaseSensitively); + } else { + cursor = mTextpage->document()->find(findEdit->text(), cursor.anchor(), QTextDocument::FindCaseSensitively); + } + + mTextpage->setTextCursor(cursor); + this->setBackground(); +} + +void FindWidget::slotFindPrevious() +{ + QTextDocument::FindFlags flags; + flags |= QTextDocument::FindBackward; + flags |= QTextDocument::FindCaseSensitively; + + QTextCursor cursor = mTextpage->textCursor(); + cursor = mTextpage->document()->find(findEdit->text(), cursor, flags); + + // if end of document is reached, restart search from beginning + if (cursor.position() == -1) { + qDebug() << "cursor:" << cursor.position(); + qDebug() << "length: " << mTextpage->document()->toPlainText().length()-1; + + cursor = mTextpage->document()->find(findEdit->text(), QTextCursor::End, QTextDocument::FindCaseSensitively); + qDebug() << "cursor2: " << cursor.position(); + QTextCursor cursor = mTextpage->document()->find(findEdit->text(), cursor, flags); + qDebug() << "cursor3: " << cursor.position(); + } + + mTextpage->setTextCursor(cursor); + this->setBackground(); +} + +void FindWidget::keyPressEvent( QKeyEvent* e ) +{ + switch ( e->key() ) + { + case Qt::Key_Escape: + this->slotClose(); + break; + case Qt::Key_F3: + if (e->modifiers() & Qt::ShiftModifier) { + this->slotFindPrevious(); + } else { + this->slotFindNext(); + } + break; + } +} + +void FindWidget::slotClose() { + QTextCursor cursor = mTextpage->textCursor(); + + if ( cursor.position() == -1) { + cursor.setPosition(0); + mTextpage->setTextCursor(cursor); + } + mTextpage->setFocus(); + close(); +} diff --git a/findwidget.h b/findwidget.h new file mode 100644 index 0000000..4e69fa6 --- /dev/null +++ b/findwidget.h @@ -0,0 +1,42 @@ + +#ifndef FINDWIDGET_H +#define FINDWIDGET_H + +#include "editorpage.h" + +#include <QWidget> + +/** + * @brief Class for handling the find widget shown at buttom of a textedit-page + */ +class FindWidget : public QWidget +{ + Q_OBJECT + +public: + /** + * @brief + * + * @param parent The parent widget + */ + explicit FindWidget(QWidget *parent, QTextEdit *edit); + +private: + void keyPressEvent( QKeyEvent* e ); + /** + * @details Set background of findEdit to red, if no match is found (Documents textcursor position equals -1), + * otherwise set it to white. + */ + void setBackground(); + + QTextEdit *mTextpage; /** Textedit associated to the notification */ + QLineEdit *findEdit; /** Label holding the text shown in verifyNotification */ + QTextCharFormat cursorFormat; + +private slots: + void slotFindNext(); + void slotFindPrevious(); + void slotFind(); + void slotClose(); +}; +#endif // FINDWIDGET_H diff --git a/gpg4usb.pro b/gpg4usb.pro index 090c79c..87fa331 100644 --- a/gpg4usb.pro +++ b/gpg4usb.pro @@ -39,6 +39,7 @@ HEADERS += attachments.h \ verifykeydetailbox.h \ wizard.h \ helppage.h \ + findwidget.h \ gpgconstants.h SOURCES += attachments.cpp \ @@ -64,6 +65,7 @@ SOURCES += attachments.cpp \ verifykeydetailbox.cpp \ wizard.cpp \ helppage.cpp \ + findwidget.cpp \ gpgconstants.cpp RC_FILE = gpg4usb.rc diff --git a/gpg4usb.qrc b/gpg4usb.qrc index 0271b67..f929b63 100644 --- a/gpg4usb.qrc +++ b/gpg4usb.qrc @@ -47,5 +47,7 @@ <file alias="mimetypes/image-x-generic.png">release/icons/mimetypes/image-x-generic.png</file> <file alias="mimetypes/text-plain.png">release/icons/mimetypes/text-plain.png</file> <file alias="mimetypes/unknown.png">release/icons/mimetypes/unknown.png</file> + <file alias="button_previous.png">release/icons/button_previous.png</file> + <file alias="button_next.png">release/icons/button_next.png</file> </qresource> </RCC> diff --git a/mainwindow.cpp b/mainwindow.cpp index 0b13aa2..63861d1 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -247,6 +247,11 @@ void MainWindow::createActions() selectallAct->setToolTip(tr("Select the whole text")); connect(selectallAct, SIGNAL(triggered()), edit, SLOT(slotSelectAll())); + findAct = new QAction(tr("&Find"), this); + findAct->setShortcut(QKeySequence::Find); + findAct->setToolTip(tr("Find a word")); + connect(findAct, SIGNAL(triggered()), this, SLOT(slotFind())); + cleanDoubleLinebreaksAct = new QAction(tr("Remove &spacing"), this); cleanDoubleLinebreaksAct->setIcon(QIcon(":format-line-spacing-triple.png")); //cleanDoubleLineBreaksAct->setShortcut(QKeySequence::SelectAll); @@ -390,6 +395,7 @@ void MainWindow::slotDisableTabActions(int number) pasteAct->setDisabled(disable); closeTabAct->setDisabled(disable); selectallAct->setDisabled(disable); + findAct->setDisabled(disable); verifyAct->setDisabled(disable); signAct->setDisabled(disable); encryptAct->setDisabled(disable); @@ -433,6 +439,8 @@ void MainWindow::createMenus() editMenu->addAction(cutAct); editMenu->addAction(pasteAct); editMenu->addAction(selectallAct); + editMenu->addAction(findAct); + editMenu->addSeparator(); editMenu->addAction(quoteAct); editMenu->addAction(cleanDoubleLinebreaksAct); editMenu->addSeparator(); @@ -837,6 +845,20 @@ void MainWindow::slotDecrypt() edit->slotFillTextEditWithText(QString::fromUtf8(*decrypted)); } +void MainWindow::slotFind() +{ + if (edit->tabCount()==0 || edit->curTextPage() == 0) { + return; + } + + // At first close verifynotification, if existing + edit->slotCurPage()->closeNoteByClass("findwidget"); + + FindWidget *fw = new FindWidget(this,edit->curTextPage()); + edit->slotCurPage()->showNotificationWidget(fw, "findWidget"); + +} + void MainWindow::slotVerify() { if (edit->tabCount()==0 || edit->slotCurPage() == 0) { diff --git a/mainwindow.h b/mainwindow.h index 9e699d3..2d6f855 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -29,6 +29,7 @@ #include "fileencryptiondialog.h" #include "settingsdialog.h" #include "verifynotification.h" +#include "findwidget.h" #include "wizard.h" QT_BEGIN_NAMESPACE @@ -106,6 +107,10 @@ private slots: void slotShowKeyDetails(); + /** + * @details Open find widget. + */ + void slotFind(); void slotStartWizard(); @@ -311,6 +316,7 @@ private: QAction *cutAct; /** Action to cut text */ QAction *pasteAct; /** Action to paste text */ QAction *selectallAct; /** Action to select whole text */ + QAction *findAct; /** Action to find text */ QAction *undoAct; /** Action to undo last action */ QAction *redoAct; /** Action to redo last action */ QAction *zoomInAct; /** Action to zoom in */ diff --git a/release/icons/button_next.png b/release/icons/button_next.png Binary files differnew file mode 100644 index 0000000..3a84a54 --- /dev/null +++ b/release/icons/button_next.png diff --git a/release/icons/button_previous.png b/release/icons/button_previous.png Binary files differnew file mode 100644 index 0000000..41bf3aa --- /dev/null +++ b/release/icons/button_previous.png @@ -92,6 +92,12 @@ public: public slots: /** + * @details Return pointer to the currently activated tabpage. + * + */ + EditorPage *slotCurPage(); + + /** * @details Insert a ">" at the begining of every line of current textedit. */ void slotQuote(); @@ -164,12 +170,6 @@ public slots: */ void slotSwitchTabDown(); - /** - * @details Return pointer to the currently activated tabpage. - * - */ - EditorPage *slotCurPage(); - private: /** * @details return just a filename stripped of a whole path |