summaryrefslogtreecommitdiff
path: root/src/urlbar.cpp
blob: d511364969d3f4254caa5869ef8f0614416b32be (plain)
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
/* ============================================================
 *
 * This file is a part of the rekonq project
 *
 * Copyright (C) 2008 by Andrea Diamantini <adjam7 at gmail dot com>
 *
 *
 * 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, or (at your option) any later version.
 *
 * 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.
 *
 * ============================================================ */

// Self Includes
#include "urlbar.h"
#include "urlbar.moc"

// Local Includes
#include "application.h"
#include "mainwindow.h"
#include "webview.h"

// Qt Includes
#include <QtCore>
#include <QtGui>


UrlBar::UrlBar(QWidget *parent)
    : KHistoryComboBox( true, parent )
    , m_webView(0)
    , m_lineEdit(new QLineEdit)
{
    setLineEdit( m_lineEdit );

    QSizePolicy policy = sizePolicy();
    setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());

    m_defaultBaseColor = palette().color( QPalette::Base );

    // add every item to history
    connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( addToHistory( const QString& ) ) );

    webViewIconChanged();
}



UrlBar::~UrlBar()
{
}



QLineEdit *UrlBar::lineEdit()
{
    return m_lineEdit;
}



void UrlBar::setWebView(WebView *webView)
{
    Q_ASSERT(!m_webView);
    m_webView = webView;
    connect(webView, SIGNAL(urlChanged(const QUrl &)), this, SLOT(webViewUrlChanged(const QUrl &)));
    connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(webViewIconChanged()));
    connect(webView, SIGNAL(iconChanged()), this, SLOT(webViewIconChanged()));
    connect(webView, SIGNAL(loadProgress(int)), this, SLOT(update()));
}



void UrlBar::webViewUrlChanged(const QUrl &url)
{
    m_lineEdit->setText(url.toString());
    m_lineEdit->setCursorPosition(0);
}



void UrlBar::webViewIconChanged()
{
    KUrl url = (m_webView)  ? m_webView->url() : KUrl();
    QIcon icon = Application::instance()->icon(url);
    QPixmap pixmap(icon.pixmap(16, 16));
    QIcon urlIcon = QIcon(pixmap);
    
    // FIXME simple hack to show Icon in the urlbar, as calling changeUrl() doesn't affect it
    insertUrl(0,urlIcon,url);
    if(count()>1)
    {
        removeItem(1);
    }
}


QLinearGradient UrlBar::generateGradient(const QColor &color) const
{
    QLinearGradient gradient(0, 0, 0, height());
    gradient.setColorAt(0, m_defaultBaseColor);
    gradient.setColorAt(0.15, color.lighter(120));
    gradient.setColorAt(0.5, color);
    gradient.setColorAt(0.85, color.lighter(120));
    gradient.setColorAt(1, m_defaultBaseColor);
    return gradient;
}


// void UrlBar::paintEvent( QPaintEvent *event )
// {
//     QPalette p = palette();
//     if (m_webView && m_webView->url().scheme() == QLatin1String("https")) 
//     {
//         QColor lightYellow(248, 248, 210);
//         p.setBrush(QPalette::Base, generateGradient(lightYellow));
//     } 
//     else 
//     {
//         p.setBrush(QPalette::Base, m_defaultBaseColor);
//     }
//     setPalette(p);
//     KHistoryComboBox::paintEvent(event);
// 
//     QPainter painter( this );
//     QRect backgroundRect = m_lineEdit->frameGeometry(); // contentsRect(); // FIXME perhaps better working with contentsRect
//     if ( m_webView && !hasFocus() )                                                               // and modifying colours..
//     {
//         int progress = m_webView->progress();
//         QColor loadingColor = QColor(116, 192, 250);
//         painter.setBrush( generateGradient(loadingColor) );
//         painter.setPen(Qt::transparent);
//         int mid = backgroundRect.width() / 100 * progress;
//         QRect progressRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height());
//         painter.drawRect(progressRect);
//     }
// }