2
* Copyright 2007 by Alex Merry <alex.merry@kdemail.net>
3
* Copyright 2008 by Alexis Ménard <darktears31@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Library General Public License version 2,
7
* or (at your option) any later version.
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
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.
25
#include <QApplication>
26
#include <QGraphicsLinearLayout>
27
#include <QGraphicsLayout>
28
#include <QGraphicsSceneDragDropEvent>
34
#include <KIconLoader>
36
#include <Plasma/Corona>
37
#include <Plasma/FrameSvg>
38
#include <Plasma/Theme>
39
#include <Plasma/AbstractToolBox>
40
#include <Plasma/View>
41
#include <Plasma/PaintUtils>
42
#include <Plasma/WindowEffects>
44
#include <kephal/screens.h>
46
using namespace Plasma;
48
class Spacer : public QGraphicsWidget
51
Spacer(QGraphicsWidget *parent)
52
: QGraphicsWidget(parent),
65
void dropEvent(QGraphicsSceneDragDropEvent *event)
67
event->setPos(mapToParent(event->pos()));
68
panel->dropEvent(event);
71
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * widget = 0)
80
//TODO: make this a pretty gradient?
81
painter->setRenderHint(QPainter::Antialiasing);
82
QPainterPath p = Plasma::PaintUtils::roundedRectangle(contentsRect().adjusted(1, 1, -2, -2), 4);
83
QColor c = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
86
painter->fillPath(p, c);
90
Panel::Panel(QObject *parent, const QVariantList &args)
91
: Containment(parent, args),
93
m_currentSize(QSize(Kephal::ScreenUtils::screenSize(screen()).width(), 35)),
102
setContainmentType(Containment::PanelContainment);
103
setDrawWallpaper(false);
105
m_background = new Plasma::FrameSvg(this);
106
m_background->setImagePath("widgets/panel-background");
107
m_background->setEnabledBorders(Plasma::FrameSvg::AllBorders);
108
connect(m_background, SIGNAL(repaintNeeded()), this, SLOT(backgroundChanged()));
110
m_lastSpaceTimer = new QTimer(this);
111
m_lastSpaceTimer->setSingleShot(true);
112
connect(m_lastSpaceTimer, SIGNAL(timeout()), this, SLOT(adjustLastSpace()));
114
m_enableUpdateResizeTimer = new QTimer(this);
115
m_enableUpdateResizeTimer->setSingleShot(true);
116
m_enableUpdateResizeTimer->setInterval(400);
117
connect(m_enableUpdateResizeTimer, SIGNAL(timeout()), this, SLOT(enableUpdateSize()));
119
m_updateSizeTimer = new QTimer(this);
120
m_updateSizeTimer->setSingleShot(true);
121
m_updateSizeTimer->setInterval(10);
122
connect(m_updateSizeTimer, SIGNAL(timeout()), this, SLOT(updateSize()));
124
connect(this, SIGNAL(appletRemoved(Plasma::Applet*)), this, SLOT(appletWasRemoved(Plasma::Applet*)));
134
//FIXME: This should be enabled, but in that case proxywidgets won't get rendered
135
//setFlag(ItemClipsChildrenToShape, true);
137
m_layout = new QGraphicsLinearLayout(this);
138
m_layout->setSpacing(4);
139
m_layout->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
140
updateBorders(geometry().toRect());
142
m_layout->setMaximumSize(size());
144
KConfigGroup cg = config("Configuration");
146
m_currentSize = cg.readEntry("minimumSize", m_currentSize);
147
if (formFactor() == Plasma::Vertical) {
148
m_currentSize.expandedTo(QSize(0, 35));
150
m_currentSize.expandedTo(QSize(35, 0));
153
setMinimumSize(cg.readEntry("minimumSize", m_currentSize));
154
setMaximumSize(cg.readEntry("maximumSize", m_currentSize));
157
QList<QAction*> Panel::contextualActions()
159
if (!m_configureAction) {
160
m_configureAction = new QAction(i18n("Panel Settings"), this);
161
m_configureAction->setIcon(KIcon("configure"));
162
connect(m_configureAction, SIGNAL(triggered()), this, SIGNAL(toolBoxToggled()));
164
constraintsEvent(Plasma::ImmutableConstraint);
167
QList<QAction*> actions;
169
actions.append(m_configureAction);
174
void Panel::backgroundChanged()
176
constraintsEvent(Plasma::LocationConstraint);
180
void Panel::adjustLastSpace()
186
bool useSpacer = true;
188
if (formFactor() == Plasma::Vertical) {
189
foreach (Applet *applet, applets()) {
190
if (applet->sizePolicy().verticalPolicy() & QSizePolicy::ExpandFlag) {
196
foreach (Applet *applet, applets()) {
197
if (applet->sizePolicy().horizontalPolicy() & QSizePolicy::ExpandFlag) {
206
m_lastSpace = new Spacer(this);
207
m_lastSpace->panel = this;
208
m_lastSpace->m_visible = false;
209
m_lastSpace->setPreferredSize(0,0);
210
m_lastSpace->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
211
m_layout->addItem(m_lastSpace);
214
m_layout->removeItem(m_lastSpace);
220
void Panel::enableUpdateSize()
223
if (m_resizedApplets > 0) {
228
void Panel::layoutApplet(Plasma::Applet* applet, const QPointF &pos)
230
// this gets called whenever an applet is added, and we add it to our layout
235
Plasma::FormFactor f = formFactor();
236
int insertIndex = -1;
238
//Enlarge the panel if possible and needed
239
QSizeF appletHint = applet->preferredSize();
240
QSizeF panelHint = m_layout->preferredSize();
241
if (f == Plasma::Horizontal) {
242
if (panelHint.width() + appletHint.width() > size().width()) {
243
resize(panelHint.width() + appletHint.width(), size().height());
246
if (panelHint.height() + appletHint.height() > size().height()) {
247
resize(size().width(), panelHint.height() + appletHint.height());
251
m_layout->setMinimumSize(size());
252
m_layout->setMaximumSize(size());
254
//if pos is (-1,-1) insert at the end of the panel
255
if (pos != QPoint(-1, -1)) {
256
for (int i = 0; i < m_layout->count(); ++i) {
257
QRectF siblingGeometry = m_layout->itemAt(i)->geometry();
258
if (f == Plasma::Horizontal) {
259
qreal middle = (siblingGeometry.left() + siblingGeometry.right()) / 2.0;
260
if (QApplication::layoutDirection() == Qt::RightToLeft) {
261
if (pos.x() > middle) {
263
} else if (pos.x() >= siblingGeometry.left()) {
266
} else if (pos.x() < middle) {
269
} else if (pos.x() <= siblingGeometry.right()) {
273
} else { // Plasma::Vertical
274
qreal middle = (siblingGeometry.top() + siblingGeometry.bottom()) / 2.0;
275
if (pos.y() < middle) {
278
} else if (pos.y() <= siblingGeometry.bottom()) {
286
m_layout->removeItem(m_lastSpace);
288
if (insertIndex == -1 || insertIndex >= m_layout->count()) {
289
m_layout->addItem(applet);
291
m_layout->insertItem(insertIndex, applet);
295
m_layout->addItem(m_lastSpace);
298
//FIXME: there must be some beter way to do this rather than this rather error prone arbitrary wait
299
m_lastSpaceTimer->start(2000);
301
connect(applet, SIGNAL(sizeHintChanged(Qt::SizeHint)), this, SLOT(delayedUpdateSize()), Qt::UniqueConnection);
304
void Panel::delayedUpdateSize()
308
if (!m_updateSizeTimer->isActive()) {
309
m_updateSizeTimer->start();
313
void Panel::appletWasRemoved(Plasma::Applet* applet)
315
disconnect(applet, SIGNAL(sizeHintChanged(Qt::SizeHint)), this, SLOT(delayedUpdateSize()));
321
m_layout->removeItem(applet);
323
//shrink the panel if possible
324
if (formFactor() == Plasma::Horizontal) {
325
resize(size().width() - applet->size().width(), size().height());
327
resize(size().width(), size().height() - applet->size().height());
330
m_layout->setMaximumSize(size());
331
m_lastSpaceTimer->start(200);
334
void Panel::updateSize()
336
if (!m_canResize || m_resizedApplets < 1) {
337
m_resizedApplets = 0;
341
m_resizedApplets = 0;
344
const bool horizontal = formFactor() != Plasma::Vertical;
345
int delta = horizontal ? size().width() : size().height();
346
foreach (Applet *applet, applets()) {
348
delta -= applet->preferredSize().width();
350
delta -= applet->preferredSize().height();
355
//setting the preferred width when delta = 0 and preferredWidth() < minimumWidth()
356
// leads to the same thing as setPreferredWidth(minimumWidth())
359
// amazing but true: preferedSize doesn't take into consideration margins.
361
m_layout->getContentsMargins(&l, &t, &r, &b);
364
setPreferredWidth(preferredWidth() + delta + l + r);
366
setPreferredHeight(preferredHeight() + delta + t + b);
370
//kDebug() << "resize to" << preferredSize() << delta << ", was" << size();
371
resize(preferredSize());
373
//for a while we won't execute updateSize() again
374
m_enableUpdateResizeTimer->start();
377
void Panel::updateBorders(const QRect &geom, bool inPaintEvent)
379
Plasma::Location loc = location();
380
FrameSvg::EnabledBorders enabledBorders = FrameSvg::AllBorders;
383
//kDebug() << loc << s << formFactor() << geometry();
385
qreal topHeight = m_background->marginSize(Plasma::TopMargin);
386
qreal bottomHeight = m_background->marginSize(Plasma::BottomMargin);
387
qreal leftWidth = m_background->marginSize(Plasma::LeftMargin);
388
qreal rightWidth = m_background->marginSize(Plasma::RightMargin);
390
//remove unwanted borders
392
// do nothing in this case, we want all the borders
393
} else if (loc == BottomEdge || loc == TopEdge) {
394
QRect r = Kephal::ScreenUtils::screenGeometry(s);
396
if (loc == BottomEdge) {
397
enabledBorders ^= FrameSvg::BottomBorder;
400
enabledBorders ^= FrameSvg::TopBorder;
404
if (geom.x() <= r.x()) {
405
enabledBorders ^= FrameSvg::LeftBorder;
408
if (geom.right() >= r.right()) {
409
enabledBorders ^= FrameSvg::RightBorder;
413
//kDebug() << "top/bottom: Width:" << width << ", height:" << height;
414
} else if (loc == LeftEdge || loc == RightEdge) {
415
QRect r = Kephal::ScreenUtils::screenGeometry(s);
417
if (loc == RightEdge) {
418
enabledBorders ^= FrameSvg::RightBorder;
421
enabledBorders ^= FrameSvg::LeftBorder;
425
if (geom.y() <= r.y()) {
426
enabledBorders ^= FrameSvg::TopBorder;
430
if (geom.bottom() >= r.bottom()) {
431
enabledBorders ^= FrameSvg::BottomBorder;
435
//kDebug() << "left/right: Width:" << width << ", height:" << height;
437
kDebug() << "no location!?";
440
//activate borders and fetch sizes again
441
m_background->setEnabledBorders(enabledBorders);
442
m_background->getMargins(leftWidth, topHeight, rightWidth, bottomHeight);
444
//calculation of extra margins has to be done after getMargins
445
const QGraphicsItem *box = toolBox();
446
if (box && immutability() == Mutable) {
447
QSizeF s = box->boundingRect().size();
448
if (formFactor() == Vertical) {
449
//hardcoded extra margin for the toolbox right now
450
bottomHeight += s.height() + 2;
451
//Default to horizontal for now
453
rightWidth += s.width() + 2;
457
//invalidate the layout and set again
459
switch (location()) {
461
rightWidth = qMin(rightWidth, qMax(qreal(2), size().width() - KIconLoader::SizeMedium));
464
leftWidth = qMin(leftWidth, qMax(qreal(2), size().width() - KIconLoader::SizeMedium));
467
bottomHeight = qMin(bottomHeight, qMax(qreal(2), size().height() - KIconLoader::SizeMedium));
470
topHeight = qMin(topHeight, qMax(qreal(2), size().height() - KIconLoader::SizeMedium));
476
m_layout->setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
479
resize(preferredSize());
484
void Panel::constraintsEvent(Plasma::Constraints constraints)
486
if (constraints & Plasma::FormFactorConstraint) {
489
Plasma::FormFactor form = formFactor();
490
Qt::Orientation layoutDirection = form == Plasma::Vertical ? Qt::Vertical : Qt::Horizontal;
491
// create or set up our layout!
493
m_layout->setMaximumSize(size());
494
m_layout->setOrientation(layoutDirection);
498
//we need to know if the width or height is 100%
499
if (constraints & Plasma::LocationConstraint || constraints & Plasma::SizeConstraint) {
501
m_currentSize = geometry().size().toSize();
502
QRectF screenRect = screen() >= 0 ? Kephal::ScreenUtils::screenGeometry(screen()) :
505
if ((formFactor() == Horizontal && m_currentSize.width() >= screenRect.width()) ||
506
(formFactor() == Vertical && m_currentSize.height() >= screenRect.height())) {
507
m_background->setElementPrefix(location());
509
switch (location()) {
511
//this call will automatically fallback at no prefix if the element isn't available
512
m_background->setElementPrefix("west-mini");
515
m_background->setElementPrefix("east-mini");
518
m_background->setElementPrefix("north-mini");
522
m_background->setElementPrefix("south-mini");
527
m_background->resizeFrame(m_currentSize);
529
//FIXME: this seems the only way to correctly resize the layout the first time when the
530
// saved panel size is less than the default is to setting a maximum size.
531
// this shouldn't happen. maybe even a qgraphicslayout bug?
532
if (m_layout && (constraints & Plasma::SizeConstraint)) {
533
m_layout->setMaximumSize(size());
536
if (constraints & Plasma::LocationConstraint) {
537
setFormFactorFromLocation(location());
541
if (constraints & Plasma::StartupCompletedConstraint) {
542
connect(this, SIGNAL(appletAdded(Plasma::Applet*,QPointF)),
543
this, SLOT(layoutApplet(Plasma::Applet*,QPointF)));
546
if (constraints & Plasma::ImmutableConstraint) {
547
bool unlocked = immutability() == Plasma::Mutable;
549
if (m_configureAction) {
550
m_configureAction->setEnabled(unlocked);
551
m_configureAction->setVisible(unlocked);
555
updateBorders(geometry().toRect());
559
void Panel::saveState(KConfigGroup &config) const
561
config.writeEntry("minimumSize", minimumSize());
562
config.writeEntry("maximumSize", maximumSize());
565
void Panel::paintInterface(QPainter *painter,
566
const QStyleOptionGraphicsItem *option,
567
const QRect& contentsRect)
569
Q_UNUSED(contentsRect)
570
//FIXME: this background drawing is bad and ugly =)
571
// draw the background untransformed (saves lots of per-pixel-math)
572
painter->resetTransform();
574
const Containment::StyleOption *containmentOpt = qstyleoption_cast<const Containment::StyleOption *>(option);
577
if (containmentOpt && containmentOpt->view) {
578
viewGeom = containmentOpt->view->geometry();
580
viewGeom = m_lastViewGeom;
583
if (m_maskDirty || m_lastViewGeom != viewGeom) {
585
m_lastViewGeom = viewGeom;
587
updateBorders(viewGeom, true);
588
if (containmentOpt && containmentOpt->view && !m_background->mask().isEmpty()) {
589
const QRegion mask = m_background->mask();
590
containmentOpt->view->setMask(mask);
591
Plasma::WindowEffects::enableBlurBehind(containmentOpt->view->winId(), true, mask);
595
// blit the background (saves all the per-pixel-products that blending does)
596
painter->setCompositionMode(QPainter::CompositionMode_Source);
597
painter->setRenderHint(QPainter::Antialiasing);
599
m_background->paintFrame(painter, option->exposedRect);
602
void Panel::setFormFactorFromLocation(Plasma::Location loc) {
606
//kDebug() << "setting horizontal form factor";
607
setFormFactor(Plasma::Horizontal);
611
//kDebug() << "setting vertical form factor";
612
setFormFactor(Plasma::Vertical);
615
//TODO: implement a form factor for floating panels
616
kDebug() << "Floating is unimplemented.";
619
kDebug() << "invalid location!!";
623
void Panel::showDropZone(const QPoint pos)
625
if (!scene() || !m_layout) {
629
if (pos == QPoint()) {
631
m_layout->removeItem(m_spacer);
637
//lucky case: the spacer is already in the right position
638
if (m_spacer && m_spacer->geometry().contains(pos)) {
642
Plasma::FormFactor f = formFactor();
643
int insertIndex = m_layout->count();
645
//FIXME: needed in two places, make it a function?
646
for (int i = 0; i < m_layout->count(); ++i) {
647
QRectF siblingGeometry = m_layout->itemAt(i)->geometry();
649
if (f == Plasma::Horizontal) {
650
qreal middle = siblingGeometry.left() + (siblingGeometry.width() / 2.0);
651
if (pos.x() < middle) {
654
} else if (pos.x() <= siblingGeometry.right()) {
658
} else { // Plasma::Vertical
659
qreal middle = siblingGeometry.top() + (siblingGeometry.height() / 2.0);
660
if (pos.y() < middle) {
663
} else if (pos.y() <= siblingGeometry.bottom()) {
670
m_spacerIndex = insertIndex;
671
if (insertIndex != -1) {
673
m_spacer = new Spacer(this);
674
m_spacer->panel = this;
676
m_layout->removeItem(m_spacer);
680
m_layout->insertItem(insertIndex, m_spacer);
684
void Panel::restore(KConfigGroup &group)
686
Containment::restore(group);
688
KConfigGroup appletsConfig(&group, "Applets");
690
QMap<int, Applet *> oderedApplets;
691
QList<Applet *> unoderedApplets;
693
foreach (Applet *applet, applets()) {
694
KConfigGroup appletConfig(&appletsConfig, QString::number(applet->id()));
695
KConfigGroup layoutConfig(&appletConfig, "LayoutInformation");
697
int order = layoutConfig.readEntry("Order", -1);
700
oderedApplets[order] = applet;
701
//if LayoutInformation is not available use the usual way, as a bonus makes it retrocompatible with oler configs
703
unoderedApplets.append(applet);
706
connect(applet, SIGNAL(sizeHintChanged(Qt::SizeHint)), this, SLOT(delayedUpdateSize()), Qt::UniqueConnection);
709
foreach (Applet *applet, oderedApplets) {
711
m_layout->insertItem(m_layout->count()-1, applet);
713
m_layout->addItem(applet);
717
foreach (Applet *applet, unoderedApplets) {
718
layoutApplet(applet, applet->pos());
724
void Panel::saveContents(KConfigGroup &group) const
726
Containment::saveContents(group);
728
KConfigGroup appletsConfig(&group, "Applets");
729
for (int order = 0; order < m_layout->count(); ++order) {
730
const Applet *applet = dynamic_cast<Applet *>(m_layout->itemAt(order));
732
KConfigGroup appletConfig(&appletsConfig, QString::number(applet->id()));
733
KConfigGroup layoutConfig(&appletConfig, "LayoutInformation");
735
layoutConfig.writeEntry("Order", order);
740
K_EXPORT_PLASMA_APPLET(panel, Panel)