konqcloseditem.cpp 4.8 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

Laurent Montel's avatar
Laurent Montel committed
35
#include <QFontDatabase>
36

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

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

K_GLOBAL_STATIC(KonqIcon, s_lightIconImage)
50

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

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

62 63
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)
64
{
65
    qDebug() << m_configGroup.name();
66 67 68 69 70
}

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

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

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

KonqClosedWindowItem::~KonqClosedWindowItem()
{
}

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

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

    float pointSize = f.pointSizeF();
    QFontMetrics fm(f);
    int w = fm.width(countStr);
101
    if (w > (oldWidth)) {
Eduardo Robles Elvira's avatar
 
Eduardo Robles Elvira committed
102 103 104 105 106 107 108 109 110 111
        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);
112

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

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

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

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

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

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

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

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

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