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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/* This file is part of the KDE project
*
* Copyright (C) 2009 by Fredy Yanardi <fyanardi@gmail.com>
* Copyright (C) 2010-2011 by Lionel Chauvin <megabigbug@yahoo.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "webshortcutwidget.h"
#include "rekonq_defines.h"
#include <QtCore/QTimer>
#include <QtCore/QSet>
#include <QtGui/QBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QFormLayout>
#include <KGlobalSettings>
#include <KIcon>
#include <KLocale>
#include <KServiceTypeTrader>
WebShortcutWidget::WebShortcutWidget(QWidget *parent)
: QDialog(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout();
QHBoxLayout *titleLayout = new QHBoxLayout();
mainLayout->addLayout(titleLayout);
QLabel *iconLabel = new QLabel(this);
KIcon wsIcon("edit-web-search");
if (wsIcon.isNull())
{
wsIcon = KIcon("preferences-web-browser-shortcuts");
}
iconLabel->setPixmap(wsIcon.pixmap(22, 22));
titleLayout->addWidget(iconLabel);
m_searchTitleLabel = new QLabel(i18n("Add Search Engine"), this);
QFont boldFont = KGlobalSettings::generalFont();
boldFont.setBold(true);
m_searchTitleLabel->setFont(boldFont);
titleLayout->addWidget(m_searchTitleLabel);
titleLayout->addStretch();
QFormLayout *formLayout = new QFormLayout();
mainLayout->addLayout(formLayout);
QFont smallFont = KGlobalSettings::smallestReadableFont();
m_nameLineEdit = new QLineEdit(this);
m_nameLineEdit->setEnabled(false);
m_nameLineEdit->setFont(smallFont);
QLabel *nameLabel = new QLabel(i18n("Name:"), this);
nameLabel->setFont(smallFont);
formLayout->addRow(nameLabel, m_nameLineEdit);
QLabel *shortcutsLabel = new QLabel(i18n("Shortcuts:"), this);
shortcutsLabel->setFont(smallFont);
m_wsLineEdit = new QLineEdit(this);
m_wsLineEdit->setMinimumWidth(100);
m_wsLineEdit->setFont(smallFont);
formLayout->addRow(shortcutsLabel, m_wsLineEdit);
connect(m_wsLineEdit, SIGNAL(textChanged(QString)), SLOT(shortcutsChanged(const QString&)));
m_noteLabel = new QLabel(this);
m_noteLabel->setFont(boldFont);
m_noteLabel->setWordWrap(true);
formLayout->addRow(m_noteLabel);
m_noteLabel->setVisible(false);
mainLayout->addStretch();
QHBoxLayout *buttonLayout = new QHBoxLayout();
mainLayout->addLayout(buttonLayout);
buttonLayout->addStretch();
m_okButton = new QPushButton(i18n("Ok"), this);
m_okButton->setDefault(true);
buttonLayout->addWidget(m_okButton);
connect(m_okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
QPushButton *cancelButton = new QPushButton(i18n("Cancel"), this);
buttonLayout->addWidget(cancelButton);
connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
setLayout(mainLayout);
setMinimumWidth(250);
m_providers = KServiceTypeTrader::self()->query("SearchProvider");
QTimer::singleShot(0, m_wsLineEdit, SLOT(setFocus()));
}
void WebShortcutWidget::showAt(const QPoint &pos)
{
adjustSize();
QPoint p;
p.setX(pos.x() - width());
p.setY(pos.y() + 10);
move(p);
QDialog::show();
}
void WebShortcutWidget::show(const KUrl &url, const QString &openSearchName, const QPoint &pos)
{
m_wsLineEdit->clear();
m_nameLineEdit->setText(openSearchName);
m_url = url;
showAt(pos);
}
void WebShortcutWidget::okClicked()
{
hide();
emit webShortcutSet(m_url, m_nameLineEdit->text(), m_wsLineEdit->text());
}
void WebShortcutWidget::cancelClicked()
{
hide();
}
void WebShortcutWidget::shortcutsChanged(const QString& newShorthands)
{
int savedCursorPosition = m_wsLineEdit->cursorPosition();
QString normalizedShorthands = QString(newShorthands).replace(QL1C(' '), QL1C(','));
m_wsLineEdit->setText(normalizedShorthands);
m_wsLineEdit->setCursorPosition(savedCursorPosition);
QSet<QString> shorthands = normalizedShorthands.split(QL1C(',')).toSet();
QString contenderName = "";
QString contenderWS = "";
Q_FOREACH(const QString &shorthand, shorthands)
{
Q_FOREACH(KService::Ptr provider, m_providers)
{
if (provider->property("Keys").toStringList().contains(shorthand))
{
contenderName = provider->property("Name").toString();
contenderWS = shorthand;
break;
}
}
}
if (!contenderName.isEmpty())
{
m_okButton->setEnabled(false);
m_noteLabel->setText(i18n("The shortcut \"%1\" is already assigned to \"%2\".", contenderWS, contenderName));
m_noteLabel->setVisible(true);
resize(minimumSize().width(), minimumSizeHint().height() + 15);
}
else
{
m_okButton->setEnabled(true);
m_noteLabel->clear();
bool noteIsVisible = m_noteLabel->isVisible();
m_noteLabel->setVisible(false);
if (noteIsVisible)
{
resize(minimumSize());
}
}
}
#include "webshortcutwidget.moc"
|