]> git.sven.stormbind.net Git - sven/vym.git/blob - src/findwidget.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / findwidget.cpp
1 #include <QAction>
2 #include <QDebug>
3 #include <QLabel>
4 #include <QLineEdit>
5 #include <QVBoxLayout>
6
7 #include <QComboBox>
8 #include <QGroupBox>
9 #include <QLabel>
10 #include <QPushButton>
11
12 #include "findwidget.h"
13 #include "mainwindow.h"
14
15 extern Main *mainWindow;
16 extern bool usingDarkTheme;
17
18 FindWidget::FindWidget(QWidget *)
19 {
20     QVBoxLayout *mainLayout = new QVBoxLayout;
21     QHBoxLayout *row2Layout = new QHBoxLayout;
22
23     QLabel *label = new QLabel;
24     label->setText(tr("Find:", "FindWidget"));
25
26     // Create LineEdit (here QComboBox)
27     findcombo = new QComboBox;
28     findcombo->setMinimumWidth(250);
29     findcombo->setEditable(true);
30
31     QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
32     findcombo->setSizePolicy(sizePolicy);
33     connect(findcombo, SIGNAL(highlighted(int)), this, SLOT(nextPressed()));
34     connect(findcombo, SIGNAL(editTextChanged(const QString &)), this,
35             SLOT(findTextChanged(const QString &)));
36
37     nextButton = new QPushButton;
38     nextButton->setIcon(QPixmap(":/find.png"));
39     // nextButton->setText (tr("Find","Find widget"));
40     connect(nextButton, SIGNAL(clicked()), this, SLOT(nextPressed()));
41
42     // QAction needed to only activate shortcut while FindWidget has focus
43     QAction *a = new QAction(nextButton->text(), this);
44     a->setShortcut(Qt::Key_Return);
45     a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
46     connect(a, SIGNAL(triggered()), this, SLOT(nextPressed()));
47     addAction(a);
48
49     filterNotesButton = new QPushButton;
50     filterNotesButton->setIcon(QPixmap(":/flag-note.svg"));
51     filterNotesButton->setCheckable(true);
52     filterNotesButton->setChecked(true);
53     connect(filterNotesButton, SIGNAL(clicked()), this, SLOT(nextPressed()));
54
55     row2Layout->addWidget(label);
56     row2Layout->addWidget(findcombo);
57     row2Layout->addWidget(nextButton);
58     row2Layout->addWidget(filterNotesButton);
59
60     mainLayout->addLayout(row2Layout);
61
62     setLayout(mainLayout);
63     status = Undefined;
64 }
65
66 QString FindWidget::getFindText() { return findcombo->currentText(); }
67
68 void FindWidget::cancelPressed()
69 {
70     hide();
71     emit(hideFindWidget()); // Restore focus
72 }
73
74 void FindWidget::nextPressed()
75 {
76     emit(nextButtonPressed(findcombo->currentText(),
77                            filterNotesButton->isChecked()));
78 }
79
80 void FindWidget::findTextChanged(const QString &) { setStatus(Undefined); }
81
82 void FindWidget::setFocus()
83 {
84     findcombo->lineEdit()->selectAll();
85     findcombo->lineEdit()->setFocus();
86 }
87
88 void FindWidget::setStatus(Status st)
89 {
90     if (st == status)
91         return;
92
93     status = st;
94     QPalette p = palette();
95     QColor c;
96     if (usingDarkTheme)
97         switch (st) {
98             case Success:
99                 c = QColor(0, 170, 0);
100                 break;
101             case Failed:
102                 c = QColor(170, 0, 0);
103                 break;
104             default:
105                 c = p.color(QPalette::Base);
106         }
107     else
108         switch (st) {
109             case Success:
110                 c = QColor(120, 255, 120);
111                 break;
112             case Failed:
113                 c = QColor(255, 120, 120);
114                 break;
115             default:
116                 c = p.color(QPalette::Base);
117         }
118     p.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(9), c);
119     p.setColor(QPalette::Inactive, static_cast<QPalette::ColorRole>(9), c);
120     findcombo->setPalette(p);
121 }