1 /*
2 * This file is part of OctoPkg, an open-source GUI for pkgng.
3 * Copyright (C) 2006 Alexandre Albuquerque Arnt
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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21 #include "searchbar.h"
22 #include "searchlineedit.h"
23 #include "uihelper.h"
24 #include <QAction>
25 #include <QHBoxLayout>
26 #include <QToolButton>
27 #include <QStyleOption>
28 #include <QPainter>
29 #include <QKeyEvent>
30 #include <QEvent>
31 #include <QSpacerItem>
32
33 /*
34 * The QWidget that holds the SearchLineEdit control and has that firefox's search sexy look!
35 */
36
SearchBar(QWidget * parent)37 SearchBar::SearchBar(QWidget *parent) :
38 QWidget(parent)
39 {
40 init();
41 }
42
43 /*
44 * Obligatory initialization code.
45 */
init()46 void SearchBar::init()
47 {
48 setVisible(false);
49 setObjectName("searchbar");
50 QHBoxLayout *layout = new QHBoxLayout(this);
51 layout->setSpacing(0);
52 layout->setMargin(4);
53
54 setStyleSheet("QWidget#searchbar{"
55 "border-top-width: .6px;"
56 "border-top-style: solid;"
57 "border-top-color: darkgray;}");
58
59 m_searchLineEdit = new SearchLineEdit(this);
60 m_searchLineEdit->setMinimumWidth(300);
61 QToolButton *m_previousButton = new QToolButton(this);
62 QToolButton *m_nextButton = new QToolButton(this);
63 m_previousButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
64 m_nextButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
65
66 QAction *m_previousAction = new QAction(this);
67 QAction *m_nextAction = new QAction(this);
68
69 m_previousAction->setText("< " + tr("Previous"));
70 m_previousButton->setAutoRaise(true);
71 m_previousAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_F3));
72 m_nextAction->setText(tr("Next") + " >");
73 m_nextButton->setAutoRaise(true);
74 m_nextAction->setShortcut(Qt::Key_F3);
75 m_previousButton->setDefaultAction(m_previousAction);
76 m_nextButton->setDefaultAction(m_nextAction);
77
78 QToolButton *tbClose = new QToolButton();
79 tbClose->setIcon(IconHelper::getIconClose());
80
81 tbClose->setAutoRaise(true);
82 tbClose->setStyleSheet("QToolButton{ font-size: 16px; font-family: verdana; border-radius: 4px; } "
83 "QToolButton:hover{ background-color: palette(light); }"
84 "QToolButton::pressed{ background-color: palette(mid); }");
85
86 tbClose->setToolTip(tr("Close"));
87 tbClose->setShortcut(Qt::Key_Escape);
88
89 layout->addWidget(tbClose, 1, Qt::AlignLeft);
90 layout->addSpacing(3);
91 layout->addWidget(m_searchLineEdit, 0, Qt::AlignLeft);
92 layout->addSpacing(2);
93 layout->addWidget(m_previousButton, 1, Qt::AlignLeft);
94 layout->addWidget(m_nextButton, 20, Qt::AlignLeft);
95
96 setLayout(layout);
97 m_searchLineEdit->setFocus();
98
99 connect(tbClose, SIGNAL(clicked()), this, SLOT(close()));
100 connect(m_searchLineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
101 connect(m_previousAction, SIGNAL(triggered()), this, SIGNAL(findPrevious()));
102 connect(m_nextAction, SIGNAL(triggered()), this, SIGNAL(findNext()));
103 }
104
105 /*
106 * Whenever the user presses the escape or clicks the close icon...
107 */
close()108 void SearchBar::close()
109 {
110 hide();
111 disconnect(m_searchLineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
112 m_searchLineEdit->setText("");
113 connect(m_searchLineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
114 emit closed();
115 }
116
117 /*
118 * Helper method to clean SearchLineEdit's content
119 */
clear()120 void SearchBar::clear()
121 {
122 m_searchLineEdit->setText("");
123 }
124
125 /*
126 * Overriden in order to get stylesheets working in QWidget derived classes
127 */
paintEvent(QPaintEvent *)128 void SearchBar::paintEvent(QPaintEvent *)
129 {
130 QStyleOption styleOption;
131 styleOption.init(this);
132 QPainter painter(this);
133 style()->drawPrimitive(QStyle::PE_Widget, &styleOption, &painter, this);
134 }
135
136 /*
137 * Overriden in order to respond to ENTER and RETURN key presses to find next itens
138 */
keyPressEvent(QKeyEvent * ke)139 void SearchBar::keyPressEvent(QKeyEvent *ke)
140 {
141 if(!m_searchLineEdit->text().isEmpty() && (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return))
142 {
143 findNext();
144 }
145 }
146
147 /*
148 * Whenever SearchBar needs to be brought to UI...
149 */
show()150 void SearchBar::show()
151 {
152 setVisible(true);
153 m_searchLineEdit->selectAll();
154 m_searchLineEdit->setFocus();
155 }
156