aboutsummaryrefslogtreecommitdiffstats
path: root/src/findwidget.cpp
blob: ff8de5a527f70fd6e9a105e26fee647a490eb18b (plain)
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
#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 (!findEdit->text().isEmpty() && mTextpage->document()->find(findEdit->text()).position() < 0 ) {
        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 = mTextpage->document()->find(findEdit->text(), cursor, QTextDocument::FindCaseSensitively);
    }

    // cursor should not stay at -1, otherwise text is not editable
    // todo: check how gedit handles this
    if(cursor.position() != -1) {
        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);
    }

    // if end of document is reached, restart search from beginning
    if (cursor.position() == -1) {
        cursor = mTextpage->document()->find(findEdit->text(), cursor, QTextDocument::FindCaseSensitively);
    }

    // cursor should not stay at -1, otherwise text is not editable
    // todo: check how gedit handles this
    if(cursor.position() != -1) {
        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 begin of document is reached, restart search from end
    if (cursor.position() == -1) {
        cursor = mTextpage->document()->find(findEdit->text(), QTextCursor::End, flags);
    }

    // cursor should not stay at -1, otherwise text is not editable
    // todo: check how gedit handles this
    if(cursor.position() != -1) {
        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();
}