konqcloseditem.cpp 4.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* This file is part of the KDE project
   Copyright 2007 David Faure <faure@kde.org>
   Copyright 2007 Eduardo Robles Elvira <edulix@gmail.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 of the License, 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.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

21
#include "konqcloseditem.h"
22
#include "konqclosedwindowsmanager.h"
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
23 24 25
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
26
#include <kconfig.h>
27
#include <QDebug>
28
#include <kglobal.h>
Laurent Montel's avatar
Laurent Montel committed
29
#include <QIcon>
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
30
#include <kiconeffect.h>
31
#include <kiconloader.h>
32
#include <konqpixmapprovider.h>
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
33
#include <kcolorscheme.h>
34

35
#include <unistd.h>
Laurent Montel's avatar
Laurent Montel committed
36
#include <QFontDatabase>
37

38 39
class KonqIcon
{
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
40 41
public:
    KonqIcon()
42
        : image(QIcon::fromTheme(QStringLiteral("konqueror")).pixmap(16).toImage())
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
43 44 45
    {
        KIconEffect::deSaturate(image, 0.60f);
    }
46

Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
47 48 49 50
    QImage image;
};

K_GLOBAL_STATIC(KonqIcon, s_lightIconImage)
51

52
KonqClosedItem::KonqClosedItem(const QString &title, const QString &group, quint64 serialNumber)
53
    : m_title(title), m_configGroup(KonqClosedWindowsManager::self()->memoryStore(), group), m_serialNumber(serialNumber)
54 55 56 57 58
{
}

KonqClosedItem::~KonqClosedItem()
{
59
    //qDebug() << "deleting group" << m_configGroup.name();
60 61 62
    m_configGroup.deleteGroup();
}

63 64
KonqClosedTabItem::KonqClosedTabItem(const QString &url, const QString &title, int pos, quint64 serialNumber)
    :  KonqClosedItem(title, "Closed_Tab" + QString::number(reinterpret_cast<qint64>(this)), serialNumber),  m_url(url), m_pos(pos)
65
{
66
    qDebug() << m_configGroup.name();
67 68 69 70 71
}

KonqClosedTabItem::~KonqClosedTabItem()
{
    m_configGroup.deleteGroup();
72
    qDebug() << "deleted group" << m_configGroup.name();
73
}
74

75 76
QPixmap KonqClosedTabItem::icon() const
{
77
    return KonqPixmapProvider::self()->pixmapFor(m_url, KIconLoader::SizeSmall);
78 79
}

80 81
KonqClosedWindowItem::KonqClosedWindowItem(const QString &title, quint64 serialNumber, int numTabs)
    :  KonqClosedItem(title, "Closed_Window" + QString::number(reinterpret_cast<qint64>(this)), serialNumber), m_numTabs(numTabs)
82
{
83
    qDebug() << m_configGroup.name();
84 85 86 87 88 89
}

KonqClosedWindowItem::~KonqClosedWindowItem()
{
}

90 91
QPixmap KonqClosedWindowItem::icon() const
{
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
92 93
    QImage overlayImg = s_lightIconImage->image.copy();
    int oldWidth = overlayImg.width();
94
    QString countStr = QString::number(m_numTabs);
95

Laurent Montel's avatar
Laurent Montel committed
96
    QFont f = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
97 98 99 100 101
    f.setBold(true);

    float pointSize = f.pointSizeF();
    QFontMetrics fm(f);
    int w = fm.width(countStr);
102
    if (w > (oldWidth)) {
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
103 104 105 106 107 108 109 110 111 112
        pointSize *= float(oldWidth) / float(w);
        f.setPointSizeF(pointSize);
    }

    // overlay
    QPainter p(&overlayImg);
    p.setFont(f);
    KColorScheme scheme(QPalette::Active, KColorScheme::Window);
    p.setPen(scheme.foreground(KColorScheme::LinkText).color());
    p.drawText(overlayImg.rect(), Qt::AlignCenter, countStr);
113

Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
114
    return QPixmap::fromImage(overlayImg);
115
}
116 117 118 119 120 121

int KonqClosedWindowItem::numTabs() const
{
    return m_numTabs;
}

122 123 124
KonqClosedRemoteWindowItem::KonqClosedRemoteWindowItem(const QString &title,
        const QString &groupName, const QString &configFileName, quint64 serialNumber,
        int numTabs, const QString &dbusService)
125
    : KonqClosedWindowItem(title, serialNumber, numTabs),
126 127
      m_remoteGroupName(groupName), m_remoteConfigFileName(configFileName),
      m_dbusService(dbusService), m_remoteConfigGroup(0L), m_remoteConfig(0L)
128
{
129
    qDebug();
130 131 132 133 134 135 136 137 138 139 140
}

KonqClosedRemoteWindowItem::~KonqClosedRemoteWindowItem()
{
    delete m_remoteConfigGroup;
    delete m_remoteConfig;
}

void KonqClosedRemoteWindowItem::readConfig() const
{
    // only do this once
141
    if (m_remoteConfig || m_remoteConfigGroup) {
142
        return;
143
    }
144

145
    m_remoteConfig = new KConfig(m_remoteConfigFileName, KConfig::SimpleConfig);
146
    m_remoteConfigGroup = new KConfigGroup(m_remoteConfig, m_remoteGroupName);
147
    qDebug();
148 149
}

150
const KConfigGroup &KonqClosedRemoteWindowItem::configGroup() const
151 152 153 154 155
{
    readConfig();
    return *m_remoteConfigGroup;
}

156
KConfigGroup &KonqClosedRemoteWindowItem::configGroup()
157 158 159 160 161
{
    readConfig();
    return *m_remoteConfigGroup;
}

162 163
bool KonqClosedRemoteWindowItem::equalsTo(const QString &groupName,
        const QString &configFileName) const
164
{
165
    return (m_remoteGroupName == groupName &&
166
            m_remoteConfigFileName == configFileName);
167
}