~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/applets/panelspacer/panelspacer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2009 Marco Martin <notmart@gmail.com>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU General Public License as
 
6
 *   published by the Free Software Foundation; either version 2,
 
7
 *   or (at your option) any later version.
 
8
 *
 
9
 *   This program is distributed in the hope that it will be useful,
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *   GNU General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "panelspacer.h"
 
21
 
 
22
#include <QPainter>
 
23
#include <QAction>
 
24
 
 
25
#include <Plasma/PaintUtils>
 
26
#include <Plasma/Containment>
 
27
#include <Plasma/Theme>
 
28
 
 
29
K_EXPORT_PLASMA_APPLET(panelspacer_internal, PanelSpacer)
 
30
 
 
31
PanelSpacer::PanelSpacer(QObject *parent, const QVariantList &args)
 
32
    : Plasma::Applet(parent, args),
 
33
      m_configurationMode(false),
 
34
      m_fixedSize(false)
 
35
{
 
36
    setAspectRatioMode(Plasma::IgnoreAspectRatio);
 
37
    setHasConfigurationInterface(false);
 
38
    QAction *toggleFixed = new QAction(i18n("Set Flexible Size"), this);
 
39
    toggleFixed->setCheckable(true);
 
40
    m_actions.append(toggleFixed);
 
41
    addAction(QLatin1String("toggle fixed"), toggleFixed);
 
42
    connect(toggleFixed, SIGNAL(toggled(bool)), this, SLOT(toggleFixed(bool)));
 
43
    setCacheMode(DeviceCoordinateCache);
 
44
}
 
45
 
 
46
PanelSpacer::~PanelSpacer()
 
47
{
 
48
}
 
49
 
 
50
QList<QAction *> PanelSpacer::contextualActions()
 
51
{
 
52
    return m_actions;
 
53
}
 
54
 
 
55
void PanelSpacer::toggleFixed(bool flexible)
 
56
{
 
57
    m_fixedSize = !flexible;
 
58
 
 
59
    if (!m_fixedSize) {
 
60
        setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
 
61
        setMinimumSize(0,0);
 
62
    } else if (formFactor() == Plasma::Horizontal) {
 
63
        setMaximumWidth(size().width());
 
64
        setMinimumWidth(size().width());
 
65
    } else if (formFactor() == Plasma::Vertical) {
 
66
        setMaximumHeight(size().height());
 
67
        setMinimumHeight(size().height());
 
68
    }
 
69
 
 
70
    config().writeEntry("FixedSize", m_fixedSize);
 
71
    emit configNeedsSaving();
 
72
}
 
73
 
 
74
void PanelSpacer::init()
 
75
{
 
76
    if (containment()) {
 
77
        connect(containment(), SIGNAL(toolBoxVisibilityChanged(bool)), this, SLOT(updateConfigurationMode(bool)));
 
78
    }
 
79
 
 
80
    configChanged();
 
81
}
 
82
 
 
83
void PanelSpacer::configChanged()
 
84
{
 
85
    m_fixedSize = config().readEntry("FixedSize", false);
 
86
    toggleFixed(!m_fixedSize);
 
87
    QAction *fixedAction = action(QLatin1String("toggle fixed"));
 
88
    if (fixedAction) {
 
89
        fixedAction->setChecked(!m_fixedSize);
 
90
    }
 
91
}
 
92
 
 
93
void PanelSpacer::constraintsEvent(Plasma::Constraints constraints)
 
94
{
 
95
    if (constraints & Plasma::FormFactorConstraint) {
 
96
        if (formFactor() == Plasma::Horizontal) {
 
97
            setMinimumWidth(minimumHeight());
 
98
            setMaximumWidth(maximumHeight());
 
99
            setMaximumHeight(QWIDGETSIZE_MAX);
 
100
            setMinimumHeight(0);
 
101
        } else if (formFactor() == Plasma::Vertical) {
 
102
            setMinimumHeight(minimumWidth());
 
103
            setMaximumHeight(maximumWidth());
 
104
            setMaximumWidth(QWIDGETSIZE_MAX);
 
105
            setMinimumWidth(0);
 
106
        }
 
107
    }
 
108
 
 
109
    if (constraints & Plasma::StartupCompletedConstraint) {
 
110
        if (!m_fixedSize) {
 
111
            setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
 
112
            setMinimumSize(0,0);
 
113
        } else if (formFactor() == Plasma::Horizontal) {
 
114
            setMaximumWidth(size().width());
 
115
            setMinimumWidth(size().width());
 
116
        } else if (formFactor() == Plasma::Vertical) {
 
117
            setMaximumHeight(size().height());
 
118
            setMinimumHeight(size().height());
 
119
        }
 
120
    }
 
121
 
 
122
    if (constraints & Plasma::SizeConstraint) {
 
123
        m_fixedSize = ((formFactor() == Plasma::Horizontal && maximumWidth() == minimumWidth()) || (formFactor() == Plasma::Vertical && maximumHeight() == minimumHeight()));
 
124
        config().writeEntry("FixedSize", m_fixedSize);
 
125
 
 
126
        QAction *fixedAction = action(QLatin1String("toggle fixed"));
 
127
        if (fixedAction) {
 
128
            fixedAction->setChecked(!m_fixedSize);
 
129
        }
 
130
    }
 
131
    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
 
132
}
 
133
 
 
134
void PanelSpacer::paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
 
135
{
 
136
    Q_UNUSED(option)
 
137
 
 
138
    if (!m_configurationMode) {
 
139
        return;
 
140
    }
 
141
    painter->setRenderHint(QPainter::Antialiasing);
 
142
    QPainterPath p = Plasma::PaintUtils::roundedRectangle(contentsRect.adjusted(1, 1, -2, -2), 4);
 
143
    QColor c = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
 
144
    c.setAlphaF(0.3);
 
145
 
 
146
    painter->fillPath(p, c);
 
147
 
 
148
    painter->setRenderHint(QPainter::Antialiasing);
 
149
    c = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
 
150
    c.setAlphaF(0.7);
 
151
 
 
152
    const int margin = 8;
 
153
 
 
154
    QPainterPath path;
 
155
    if (formFactor() == Plasma::Horizontal) {
 
156
        path = Plasma::PaintUtils::roundedRectangle(contentsRect.adjusted(1, 1, -contentsRect.width()+margin-1, -2), 4);
 
157
        painter->fillPath(path, c);
 
158
        path = Plasma::PaintUtils::roundedRectangle(contentsRect.adjusted(contentsRect.width()-margin, 1, -2, -2), 4);
 
159
    } else if (formFactor() == Plasma::Vertical) {
 
160
        path = Plasma::PaintUtils::roundedRectangle(contentsRect.adjusted(1, 1, -2, -contentsRect.height()+margin-1), 4);
 
161
        painter->fillPath(path, c);
 
162
        path = Plasma::PaintUtils::roundedRectangle(contentsRect.adjusted(1, contentsRect.height()-margin, -2, -2), 4);
 
163
    }
 
164
    painter->fillPath(path, c);
 
165
}
 
166
 
 
167
void PanelSpacer::updateConfigurationMode(bool config)
 
168
{
 
169
    if (containment() && containment()->immutability() != Plasma::Mutable) {
 
170
        config = false;
 
171
    }
 
172
 
 
173
    if (config != m_configurationMode) {
 
174
        m_configurationMode = config;
 
175
        update();
 
176
    }
 
177
}
 
178
 
 
179
 
 
180
#include "panelspacer.moc"
 
181