summaryrefslogtreecommitdiff
path: root/src/stackedurlbar.cpp
blob: 04b702f58b0430bf3ab33a37d1fc6858d9e19094 (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
144
145
146
147
148
149
150
151
152
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2009 by Paweł Prażak <pawelprazak 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 3, 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 "stackedurlbar.h"
#include "stackedurlbar.moc"

// Local Includes
#include "application.h"
#include "history.h"
#include "urlbar.h"

// KDE Includes
#include <KDebug>


StackedUrlBar::StackedUrlBar(QWidget *parent)
        : QStackedWidget(parent)
        , m_completion(0)
        , m_completionModel(0)
{
}


StackedUrlBar::~StackedUrlBar()
{
    delete m_completion;
    delete m_completionModel;
}


UrlBar *StackedUrlBar::currentUrlBar()
{
    return urlBar(currentIndex());
}


UrlBar *StackedUrlBar::urlBar(int index)
{
    UrlBar *urlBar = qobject_cast<UrlBar*>(QStackedWidget::widget(index));
    if (!urlBar)
    {
        kWarning() << "URL bar with index" << index << "not found. Returning NULL.  line:" << __LINE__;
    }

    return urlBar;
}


void StackedUrlBar::addUrlBar(UrlBar* urlBar)
{
    QStackedWidget::addWidget(urlBar);

    // setup completion objects
    urlBar->setCompletionObject(completion());
}


void StackedUrlBar::setCurrentUrlBar(UrlBar* urlBar)
{
    QStackedWidget::setCurrentWidget(urlBar);
}


void StackedUrlBar::removeUrlBar(UrlBar* urlBar)
{
    QStackedWidget::removeWidget(urlBar);
}


void StackedUrlBar::clear()
{
    currentUrlBar()->clearHistory();

    for (int i = 0; i < count(); ++i)
    {
        urlBar(i)->clear();
    }
}


QList<const UrlBar* > StackedUrlBar::urlBars()
{
    QList<const UrlBar *> list;
    for (int i = 0; i < count(); ++i)
    {
        const UrlBar* u = urlBar(i);
        list.append(u);
    }
    return list;
}


KCompletion *StackedUrlBar::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 *StackedUrlBar::completionModel()
{
    if (!m_completionModel)
    {
        m_completionModel = new HistoryCompletionModel(this);
        m_completionModel->setSourceModel(Application::historyManager()->historyFilterModel());
    }
    return m_completionModel;
}