summaryrefslogtreecommitdiff
path: root/src/historydialog.cpp
blob: 6a1509525daebf5b101038b4d2fbb1aaed785720 (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
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2009 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 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.
*
* ============================================================ */


// Auto Includes
#include "historydialog.h"
#include "historydialog.moc"

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

// KDE Includes
#include <KAction>
#include <KUrl>

// Qt Includes
#include <QtCore/QPoint>

#include <QtGui/QWidget>
#include <QtGui/QClipboard>
#include <QtGui/QDesktopWidget>


HistoryDialog::HistoryDialog(QWidget *parent, HistoryManager *setHistory) 
    : KDialog(parent)
    , m_historyWidg(new Ui::historyWidget)
{
    HistoryManager *history = setHistory;
    if (!history)
        history = Application::historyManager();

    setCaption( i18n("History") );
    setButtons( KDialog::Close );

    QWidget *widget = new QWidget;
    m_historyWidg->setupUi(widget);
    setMainWidget(widget);

    m_historyWidg->search->setClearButtonShown(true);

    m_historyWidg->tree->setUniformRowHeights(true);
    m_historyWidg->tree->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_historyWidg->tree->setTextElideMode(Qt::ElideMiddle);

    QAbstractItemModel *model = history->historyTreeModel();
    TreeProxyModel *proxyModel = new TreeProxyModel(this);

    connect(m_historyWidg->search, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterFixedString(QString)));
    connect(m_historyWidg->removeButton, SIGNAL(clicked()), m_historyWidg->tree, SLOT(removeOne()));
    connect(m_historyWidg->removeAllButton, SIGNAL(clicked()), history, SLOT(clear()));

    proxyModel->setSourceModel(model);
    m_historyWidg->tree->setModel(proxyModel);
    m_historyWidg->tree->setExpanded(proxyModel->index(0, 0), true);
    m_historyWidg->tree->setAlternatingRowColors(true);

    QFontMetrics fm(font());
    int header = fm.width(QLatin1Char('m')) * 25;
    m_historyWidg->tree->header()->resizeSection(0, header);
    m_historyWidg->tree->header()->setStretchLastSection(true);

    m_historyWidg->tree->setContextMenuPolicy(Qt::CustomContextMenu);

    connect(m_historyWidg->tree, SIGNAL(customContextMenuRequested(const QPoint &)), 
            this, SLOT(customContextMenuRequested(const QPoint &)));

    connect(m_historyWidg->tree, SIGNAL(activated(const QModelIndex&)), this, SLOT(open()));
}


void HistoryDialog::customContextMenuRequested(const QPoint &pos)
{
    KMenu menu;
    QModelIndex index = m_historyWidg->tree->indexAt(pos);
    index = index.sibling(index.row(), 0);
    if (index.isValid() && !m_historyWidg->tree->model()->hasChildren(index))
    {
        menu.addAction(i18n("Open"), this, SLOT(open()));
        menu.addSeparator();
        menu.addAction(i18n("Copy"), this, SLOT(copy()));
    }
    menu.addAction(i18n("Delete"), m_historyWidg->tree, SLOT(removeOne()));
    menu.exec(QCursor::pos());
}


void HistoryDialog::open()
{
    QModelIndex index = m_historyWidg->tree->currentIndex();
    if (!index.parent().isValid())
        return;
    emit openUrl(index.data(HistoryModel::UrlRole).toUrl());
}


void HistoryDialog::copy()
{
    QModelIndex index = m_historyWidg->tree->currentIndex();
    if (!index.parent().isValid())
        return;
    QString url = index.data(HistoryModel::UrlStringRole).toString();

    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setText(url);
}

QSize HistoryDialog::sizeHint() const
{
    QRect desktopRect = Application::desktop()->screenGeometry();
    QSize size = desktopRect.size() * 0.7;
    return size;
}