1 /* ============================================================
2 * QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
3 * Copyright (C) 2011-2020 QuiteRSS Team <quiterssteam@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #include "findtext.h"
19 
FindTextContent(QWidget * parent)20 FindTextContent::FindTextContent(QWidget *parent)
21   : QLineEdit(parent)
22 {
23   findInNewsAct_ = new QAction(this);
24   findInNewsAct_->setObjectName("findInNewsAct");
25   findInNewsAct_->setIcon(QIcon(":/images/findInNews"));
26   findInNewsAct_->setCheckable(true);
27   findInNewsAct_->setChecked(true);
28 
29   findTitleAct_ = new QAction(this);
30   findTitleAct_->setObjectName("findTitleAct");
31   findTitleAct_->setIcon(QIcon(":/images/findInNews"));
32   findTitleAct_->setCheckable(true);
33   findAuthorAct_ = new QAction(this);
34   findAuthorAct_->setObjectName("findAuthorAct");
35   findAuthorAct_->setIcon(QIcon(":/images/findInNews"));
36   findAuthorAct_->setCheckable(true);
37   findCategoryAct_ = new QAction(this);
38   findCategoryAct_->setObjectName("findCategoryAct");
39   findCategoryAct_->setIcon(QIcon(":/images/findInNews"));
40   findCategoryAct_->setCheckable(true);
41   findContentAct_ = new QAction(this);
42   findContentAct_->setObjectName("findContentAct");
43   findContentAct_->setIcon(QIcon(":/images/findInNews"));
44   findContentAct_->setCheckable(true);
45   findLinkAct_ = new QAction(this);
46   findLinkAct_->setObjectName("findLinkAct");
47   findLinkAct_->setIcon(QIcon(":/images/findInNews"));
48   findLinkAct_->setCheckable(true);
49 
50   findInBrowserAct_ = new QAction(this);
51   findInBrowserAct_->setObjectName("findInBrowserAct");
52   findInBrowserAct_->setIcon(QIcon(":/images/findText"));
53   findInBrowserAct_->setCheckable(true);
54 
55   findGroup_ = new QActionGroup(this);
56   findGroup_->setExclusive(true);
57   findGroup_->addAction(findInNewsAct_);
58   findGroup_->addAction(findTitleAct_);
59   findGroup_->addAction(findAuthorAct_);
60   findGroup_->addAction(findCategoryAct_);
61   findGroup_->addAction(findContentAct_);
62   findGroup_->addAction(findLinkAct_);
63   findGroup_->addAction(findInBrowserAct_);
64 
65   findMenu_ = new QMenu(this);
66   findMenu_->addActions(findGroup_->actions());
67   findMenu_->insertSeparator(findTitleAct_);
68   findMenu_->insertSeparator(findInBrowserAct_);
69 
70   findButton_ = new QToolButton(this);
71   findButton_->setFocusPolicy(Qt::NoFocus);
72   QPixmap findPixmap(":/images/selectFindInNews");
73   findButton_->setIcon(QIcon(findPixmap));
74   findButton_->setIconSize(findPixmap.size());
75   findButton_->setCursor(Qt::ArrowCursor);
76   findButton_->setStyleSheet("QToolButton { border: none; padding: 0px; background: none; }");
77 
78   connect(findButton_, SIGNAL(clicked()), this, SLOT(slotMenuFind()));
79   connect(findGroup_, SIGNAL(triggered(QAction*)),
80           this, SLOT(slotSelectFind(QAction*)));
81 
82   clearButton_ = new QToolButton(this);
83   clearButton_->setFocusPolicy(Qt::NoFocus);
84   QPixmap pixmap(":/images/editClear");
85   clearButton_->setIcon(QIcon(pixmap));
86   clearButton_->setIconSize(pixmap.size());
87   clearButton_->setCursor(Qt::ArrowCursor);
88   clearButton_->setStyleSheet("QToolButton { border: none; padding: 0px; background: none; }");
89   clearButton_->hide();
90   connect(clearButton_, SIGNAL(clicked()), this, SLOT(slotClear()));
91   connect(this, SIGNAL(textChanged(const QString&)),
92           SLOT(updateClearButton(const QString&)));
93 
94   findLabel_ = new QLabel(tr("Find in News"), this);
95   findLabel_->setStyleSheet("QLabel { color: gray; background: none; }");
96 
97   int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
98   setStyleSheet(QString("QLineEdit { padding-right: %1px; padding-left: %2px; }").
99                 arg(clearButton_->sizeHint().width() + frameWidth + 1).
100                 arg(findButton_->sizeHint().width() + frameWidth + 1));
101   QSize msz = minimumSizeHint();
102   setMinimumSize(
103         qMax(msz.width(), clearButton_->sizeHint().height() + findButton_->sizeHint().height() + frameWidth * 2 + 2),
104         qMax(msz.height(), clearButton_->sizeHint().height() + frameWidth * 2 + 2));
105 }
106 
retranslateStrings()107 void FindTextContent::retranslateStrings()
108 {
109   findInNewsAct_->setText(tr("Find in News"));
110   findTitleAct_->setText(tr("Find in Titles"));
111   findAuthorAct_->setText(tr("Find in Authors"));
112   findCategoryAct_->setText(tr("Find in Categories"));
113   findContentAct_->setText(tr("Find in Descriptions"));
114   findLinkAct_->setText(tr("Find in Links"));
115   findInBrowserAct_->setText(tr("Find in Browser"));
116   findLabel_->setText(findGroup_->checkedAction()->text());
117   if (findLabel_->isVisible()) {
118     findLabel_->hide();
119     findLabel_->show();
120   }
121 }
122 
keyPressEvent(QKeyEvent * event)123 void FindTextContent::keyPressEvent(QKeyEvent *event)
124 {
125   if (event->key() == Qt::Key_Escape) {
126     emit signalVisible(false);
127   }
128   QLineEdit::keyPressEvent(event);
129 }
130 
resizeEvent(QResizeEvent *)131 void FindTextContent::resizeEvent(QResizeEvent *)
132 {
133   int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
134   QSize sz = findButton_->sizeHint();
135   findButton_->move(frameWidth+3,
136                    (rect().bottom() + 1 - sz.height())/2);
137   sz = findLabel_->sizeHint();
138   findLabel_->move(frameWidth+findButton_->sizeHint().width()+5,
139                    (rect().bottom() + 1 - sz.height())/2);
140   sz = clearButton_->sizeHint();
141     clearButton_->move(rect().right() - frameWidth - sz.width(),
142                       (rect().bottom() + 1 - sz.height())/2);
143 }
144 
focusInEvent(QFocusEvent * event)145 void FindTextContent::focusInEvent(QFocusEvent *event)
146 {
147   findLabel_->setVisible(false);
148   QLineEdit::focusInEvent(event);
149 }
150 
focusOutEvent(QFocusEvent * event)151 void FindTextContent::focusOutEvent(QFocusEvent *event)
152 {
153   if (text().isEmpty())
154     findLabel_->setVisible(true);
155   QLineEdit::focusOutEvent(event);
156 }
157 
updateClearButton(const QString & text)158 void FindTextContent::updateClearButton(const QString& text)
159 {
160   clearButton_->setVisible(!text.isEmpty());
161   if (!hasFocus())
162     findLabel_->setVisible(true);
163 }
164 
slotClear()165 void FindTextContent::slotClear()
166 {
167   clear();
168   emit signalClear();
169 }
170 
slotMenuFind()171 void FindTextContent::slotMenuFind()
172 {
173   findMenu_->popup(mapToGlobal(QPoint(0, height()-1)));
174 }
175 
slotSelectFind(QAction * act)176 void FindTextContent::slotSelectFind(QAction *act)
177 {
178   if (act->objectName() == "findInBrowserAct") {
179     findButton_->setIcon(QIcon(":/images/selectFindInBrowser"));
180   } else {
181     findButton_->setIcon(QIcon(":/images/selectFindInNews"));
182   }
183   findLabel_->setText(act->text());
184   emit signalSelectFind();
185 }
186