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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2008-2009 by Andrea Diamantini <adjam7 at gmail dot com>
* Copyright (C) 2009 by Domrachev Alexandr <alexandr.domrachev@gmail.com>
* Copyright (C) 2009 by Paweł Prażak <pawelprazak at gmail dot com>
* Copyright (C) 2009 by Lionel Chauvin <megabigbug@yahoo.fr>
*
*
* This program 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================ */
// Self Includes
#include "urlbar.h"
#include "urlbar.moc"
// Local Includes
#include "application.h"
#include "history.h"
#include "lineedit.h"
#include "mainwindow.h"
#include "webview.h"
// KDE Includes
#include <KDebug>
#include <KCompletionBox>
#include <KUrl>
// Qt Includes
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QPalette>
QColor UrlBar::s_defaultBaseColor;
UrlBar::UrlBar(QWidget *parent)
: KHistoryComboBox(true, parent)
, m_lineEdit(new LineEdit)
, m_progress(0)
, m_completion(0)
, m_completionModel(0)
{
setUrlDropsEnabled(true);
setAutoDeleteCompletionObject(true);
//cosmetic
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setMinimumWidth(180);
setTrapReturnKey(true);
setupLineEdit();
// add every item to history
connect(this, SIGNAL(returnPressed(const QString&)), SLOT(slotActivated(const QString&)));
connect(completionBox(), SIGNAL(activated(const QString&)), SLOT(slotActivated(const QString&)));
connect(this, SIGNAL(cleared()), SLOT(slotCleared()));
// setup completion box
completionBox()->setTabHandling(true); // Konqueror bug #167135
setCompletionObject(completion());
// set dropdown list background
QPalette p = view()->palette();
p.setColor(QPalette::Base, palette().color(QPalette::Base));
view()->setPalette(p);
/*
// set empty item with default icon
slotUpdateUrl();*/
}
UrlBar::~UrlBar()
{
}
void UrlBar::selectAll() const
{
lineEdit()->selectAll();
}
KUrl UrlBar::url() const
{
return m_currentUrl;
}
KLineEdit *UrlBar::lineEdit() const
{
return m_lineEdit;
}
void UrlBar::setupLineEdit()
{
// Make m_lineEdit background transparent
QPalette p = m_lineEdit->palette();
p.setColor(QPalette::Base, Qt::transparent);
m_lineEdit->setPalette(p);
if (!s_defaultBaseColor.isValid())
{
s_defaultBaseColor = palette().color(QPalette::Base);
}
setLineEdit(m_lineEdit);
// Make the lineedit consume the Qt::Key_Enter event...
lineEdit()->setTrapReturnKey(true);
lineEdit()->setHandleSignals(true);
// clear the URL bar
lineEdit()->clear();
}
void UrlBar::setUrl(const QUrl& url)
{
m_currentUrl = url;
slotUpdateUrl();
}
void UrlBar::setProgress(int progress)
{
m_progress = progress;
repaint();
}
void UrlBar::slotUpdateUrl()
{
if (count())
{
changeUrl(0, Application::icon(m_currentUrl), m_currentUrl);
}
else
{
insertUrl(0, Application::icon(m_currentUrl), m_currentUrl);
}
setCurrentIndex(0);
// important security consideration: always display the beginning
// of the url rather than its end to prevent spoofing attempts.
// Must be AFTER setCurrentIndex
if (!hasFocus())
{
lineEdit()->setCursorPosition(0);
}
}
void UrlBar::slotActivated(const QString& url)
{
if (url.isEmpty())
return;
setUrl(url);
Application::historyManager()->addHistoryEntry(url);
emit activated(m_currentUrl);
}
void UrlBar::slotCleared()
{
// clear the history on user's request from context menu
clear();
Application::historyManager()->clear();
}
void UrlBar::slotLoadFinished(bool)
{
// reset progress bar after small delay
m_progress = 0;
QTimer::singleShot(200, this, SLOT(repaint()));
}
void UrlBar::slotUpdateProgress(int progress)
{
m_progress = progress;
repaint();
}
void UrlBar::paintEvent(QPaintEvent *event)
{
// set background color of UrlBar
QPalette p = palette();
p.setColor(QPalette::Base, s_defaultBaseColor);
setPalette(p);
KHistoryComboBox::paintEvent(event);
if (!hasFocus())
{
QPainter painter(this);
QColor loadingColor;
if (m_currentUrl.scheme() == QLatin1String("https"))
{
loadingColor = QColor(248, 248, 100);
}
else
{
loadingColor = QColor(116, 192, 250);
}
painter.setBrush(generateGradient(loadingColor, height()));
painter.setPen(Qt::transparent);
QRect backgroundRect = lineEdit()->frameGeometry();
int mid = backgroundRect.width() * m_progress / 100;
QRect progressRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height());
painter.drawRect(progressRect);
painter.end();
}
}
void UrlBar::focusOutEvent(QFocusEvent *event)
{
// set back last loaded url in case user cleared it
if (!m_currentUrl.equals(KUrl(lineEdit()->text()))) setUrl(m_currentUrl);
KHistoryComboBox::focusOutEvent(event);
}
QSize UrlBar::sizeHint() const
{
return lineEdit()->sizeHint();
}
QLinearGradient UrlBar::generateGradient(const QColor &color, int height)
{
QColor base = s_defaultBaseColor;
base.setAlpha(0);
QColor barColor = color;
barColor.setAlpha(200);
QLinearGradient gradient(0, 0, 0, height);
gradient.setColorAt(0, base);
gradient.setColorAt(0.25, barColor.lighter(120));
gradient.setColorAt(0.5, barColor);
gradient.setColorAt(0.75, barColor.lighter(120));
gradient.setColorAt(1, base);
return gradient;
}
void UrlBar::setBackgroundColor(QColor c)
{
s_defaultBaseColor=c;
repaint();
}
bool UrlBar::isLoading()
{
if(m_progress == 0)
{
return false;
}
return true;
}
KCompletion *UrlBar::completion()
{
// make sure completion was created
if (!m_completion)
{
m_completion = new KCompletion();
m_completion->setCompletionMode(KGlobalSettings::CompletionPopupAuto);
m_completion->setOrder(KCompletion::Weighted);
m_completion->setIgnoreCase(true);
kDebug() << "Initialize completion list...";
HistoryCompletionModel *model = completionModel();
int count = model->rowCount();
kDebug() << "...initialize history items" << count;
// change order to insertion to avoid confusion of the addItem method
// in weighted it expects format string:number and it thinks http it the whole string
m_completion->setOrder(KCompletion::Insertion);
for (int i = 0; i < count; ++i)
{
QString item = model->data(model->index(i, 0)).toString();
item.remove(QRegExp("^http://|/$"));
m_completion->addItem(item);
}
m_completion->setOrder(KCompletion::Weighted);
}
return m_completion;
}
HistoryCompletionModel *UrlBar::completionModel()
{
if (!m_completionModel)
{
m_completionModel = new HistoryCompletionModel(this);
m_completionModel->setSourceModel(Application::historyManager()->historyFilterModel());
}
return m_completionModel;
}
|