load and save toolbox position between restarts!

still to come:

* don't paint when zoomed, replace with tool strip instead
* respect available area during drag
* smarter positioning (right now it just loads the saved position whenever it needs to reposition)

svn path=/trunk/KDE/kdelibs/; revision=884072
This commit is contained in:
Aaron J. Seigo 2008-11-14 07:28:02 +00:00
parent 18885bd0f0
commit d0c7968b18
7 changed files with 163 additions and 84 deletions

View File

@ -307,6 +307,10 @@ void Containment::save(KConfigGroup &g) const
group.writeEntry("location", (int)d->location);
group.writeEntry("activity", d->context()->currentActivity());
if (d->toolBox) {
d->toolBox->save(group);
}
if (d->wallpaper) {
group.writeEntry("wallpaperplugin", d->wallpaper->pluginName());
group.writeEntry("wallpaperpluginmode", d->wallpaper->renderingMode().name());
@ -1476,6 +1480,7 @@ ToolBox *ContainmentPrivate::createToolBox()
if (toolBox) {
QObject::connect(toolBox, SIGNAL(toggled()), q, SIGNAL(toolBoxToggled()));
toolBox->load();
positionToolBox();
}
}
@ -1485,73 +1490,8 @@ ToolBox *ContainmentPrivate::createToolBox()
void ContainmentPrivate::positionToolBox()
{
if (!toolBox) {
return;
}
kDebug();
//The placement assumes that the geometry width/height is no more than the screen
if (type == Containment::PanelContainment) {
if (q->formFactor() == Vertical) {
toolBox->setCorner(ToolBox::Bottom);
toolBox->setPos(q->geometry().width() / 2 - toolBox->boundingRect().width() / 2,
q->geometry().height());
} else {
//defaulting to Horizontal right now
if (QApplication::layoutDirection() == Qt::RightToLeft) {
toolBox->setPos(q->geometry().left(),
q->geometry().height() / 2 - toolBox->boundingRect().height() / 2);
toolBox->setCorner(ToolBox::Left);
} else {
toolBox->setPos(q->geometry().width(),
q->geometry().height() / 2 - toolBox->boundingRect().height() / 2);
toolBox->setCorner(ToolBox::Right);
}
}
} else if (q->corona()) {
kDebug() << "desktop";
QRectF avail = q->corona()->availableScreenRegion(screen).boundingRect();
QRectF screenGeom = q->corona()->screenGeometry(screen);
// Transform to the containment's coordinate system.
avail.translate(-screenGeom.topLeft());
screenGeom.moveTo(0, 0);
const int toolBoxSize = toolBox->size();
if (!q->view() || !q->view()->transform().isScaling()) {
if (QApplication::layoutDirection() == Qt::RightToLeft) {
if (avail.top() > screenGeom.top()) {
toolBox->setPos(avail.topLeft() - QPoint(0, toolBoxSize));
toolBox->setCorner(ToolBox::Left);
} else if (avail.left() > screenGeom.left()) {
toolBox->setPos(avail.topLeft() - QPoint(toolBoxSize, 0));
toolBox->setCorner(ToolBox::Top);
} else {
toolBox->setPos(avail.topLeft());
toolBox->setCorner(ToolBox::TopLeft);
}
} else {
if (avail.top() > screenGeom.top()) {
toolBox->setPos(avail.topRight() - QPoint(0, toolBoxSize));
toolBox->setCorner(ToolBox::Right);
} else if (avail.right() < screenGeom.right()) {
toolBox->setPos(avail.topRight() - QPoint(toolBoxSize, 0));
toolBox->setCorner(ToolBox::Top);
} else {
toolBox->setPos(avail.topRight() - QPoint(toolBoxSize, 0));
toolBox->setCorner(ToolBox::TopRight);
}
}
} else {
if (QApplication::layoutDirection() == Qt::RightToLeft) {
toolBox->setPos(q->mapFromScene(QPointF(q->geometry().topLeft())));
toolBox->setCorner(ToolBox::TopLeft);
} else {
toolBox->setPos(q->mapFromScene(QPointF(q->geometry().topRight())));
toolBox->setCorner(ToolBox::TopRight);
}
}
if (toolBox) {
toolBox->reposition();
}
}

View File

@ -103,7 +103,7 @@ public:
bool hovering : 1;
};
DesktopToolBox::DesktopToolBox(QGraphicsItem *parent)
DesktopToolBox::DesktopToolBox(Containment *parent)
: ToolBox(parent),
d(new DesktopToolBoxPrivate)
{

View File

@ -43,7 +43,7 @@ class DesktopToolBox : public ToolBox
Q_OBJECT
public:
explicit DesktopToolBox(QGraphicsItem *parent = 0);
explicit DesktopToolBox(Containment *parent = 0);
~DesktopToolBox();
QRectF boundingRect() const;
QPainterPath shape() const;

View File

@ -95,7 +95,7 @@ public:
bool toggled;
};
PanelToolBox::PanelToolBox(QGraphicsItem *parent)
PanelToolBox::PanelToolBox(Containment *parent)
: ToolBox(parent),
d(new PanelToolBoxPrivate)
{

View File

@ -43,7 +43,7 @@ class PanelToolBox : public ToolBox
Q_OBJECT
public:
explicit PanelToolBox(QGraphicsItem *parent = 0);
explicit PanelToolBox(Containment *parent);
~PanelToolBox();
QRectF boundingRect() const;
QPainterPath shape() const;

View File

@ -21,14 +21,18 @@
#include "toolbox_p.h"
#include <QAction>
#include <QApplication>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsView>
#include <QPainter>
#include <QRadialGradient>
#include <kcolorscheme.h>
#include <kconfiggroup.h>
#include <kdebug.h>
#include <plasma/theme.h>
#include "corona.h"
#include "theme.h"
#include "widgets/iconwidget.h"
namespace Plasma
@ -37,16 +41,18 @@ namespace Plasma
class ToolBoxPrivate
{
public:
ToolBoxPrivate()
: size(50),
iconSize(32, 32),
corner(ToolBox::TopRight),
hidden(false),
showing(false),
movable(false),
dragging(false)
ToolBoxPrivate(Containment *c)
: containment(c),
size(50),
iconSize(32, 32),
corner(ToolBox::TopRight),
hidden(false),
showing(false),
movable(false),
dragging(false)
{}
Containment *containment;
int size;
QSize iconSize;
ToolBox::Corner corner;
@ -55,11 +61,12 @@ public:
bool showing : 1;
bool movable : 1;
bool dragging : 1;
bool userMoved : 1;
};
ToolBox::ToolBox(QGraphicsItem *parent)
ToolBox::ToolBox(Containment *parent)
: QGraphicsItem(parent),
d(new ToolBoxPrivate)
d(new ToolBoxPrivate(parent))
{
setAcceptsHoverEvents(true);
}
@ -194,6 +201,7 @@ void ToolBox::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
// sticky points at midpoints
// change how buttons appear depending on the location of the box
d->dragging = true;
d->userMoved = true;
const QPoint newPos = mapToParent(event->pos()).toPoint();
const QPoint curPos = pos().toPoint();
const int h = abs(boundingRect().height());
@ -284,6 +292,131 @@ void ToolBox::setIsMovable(bool movable)
d->movable = movable;
}
void ToolBox::save(KConfigGroup &cg) const
{
if (!d->movable) {
return;
}
KConfigGroup group(&cg, "ToolBox");
if (!d->userMoved) {
group.deleteGroup();
return;
}
int offset = 0;
if (d->corner == ToolBox::Left ||
d->corner == ToolBox::Right) {
offset = y();
} else if (d->corner == ToolBox::Left ||
d->corner == ToolBox::Right) {
offset = x();
}
group.writeEntry("corner", int(d->corner));
group.writeEntry("offset", offset);
}
void ToolBox::load()
{
if (!d->movable) {
return;
}
KConfigGroup group = d->containment->config();
group = KConfigGroup(&group, "ToolBox");
if (!group.hasKey("corner")) {
return;
}
d->userMoved = true;
d->corner = Corner(group.readEntry("corner", int(d->corner)));
int offset = group.readEntry("offset", 0);
if (d->corner == ToolBox::Left) {
setPos(0, offset);
} else if (d->corner == ToolBox::Right) {
setPos(d->containment->size().width() - d->size, offset);
} else if (d->corner == ToolBox::Top) {
setPos(offset, 0);
} else if (d->corner == ToolBox::Bottom) {
setPos(offset, d->containment->size().height() - d->size);
}
}
void ToolBox::reposition()
{
if (d->userMoved) {
//FIXME: adjust for situations like changing of the available space
load();
return;
}
if (d->containment->containmentType() == Containment::PanelContainment) {
if (d->containment->formFactor() == Vertical) {
setCorner(ToolBox::Bottom);
setPos(d->containment->geometry().width() / 2 - boundingRect().width() / 2,
d->containment->geometry().height());
} else {
//defaulting to Horizontal right now
if (QApplication::layoutDirection() == Qt::RightToLeft) {
setPos(d->containment->geometry().left(),
d->containment->geometry().height() / 2 - boundingRect().height() / 2);
setCorner(ToolBox::Left);
} else {
setPos(d->containment->geometry().width(),
d->containment->geometry().height() / 2 - boundingRect().height() / 2);
setCorner(ToolBox::Right);
}
}
} else if (d->containment->corona()) {
//kDebug() << "desktop";
int screen = d->containment->screen();
QRectF avail = d->containment->corona()->availableScreenRegion(screen).boundingRect();
QRectF screenGeom = d->containment->corona()->screenGeometry(screen);
// Transform to the containment's coordinate system.
avail.translate(-screenGeom.topLeft());
screenGeom.moveTo(0, 0);
if (!d->containment->view() || !d->containment->view()->transform().isScaling()) {
if (QApplication::layoutDirection() == Qt::RightToLeft) {
if (avail.top() > screenGeom.top()) {
setPos(avail.topLeft() - QPoint(0, d->size));
setCorner(ToolBox::Left);
} else if (avail.left() > screenGeom.left()) {
setPos(avail.topLeft() - QPoint(d->size, 0));
setCorner(ToolBox::Top);
} else {
setPos(avail.topLeft());
setCorner(ToolBox::TopLeft);
}
} else {
if (avail.top() > screenGeom.top()) {
setPos(avail.topRight() - QPoint(0, d->size));
setCorner(ToolBox::Right);
} else if (avail.right() < screenGeom.right()) {
setPos(avail.topRight() - QPoint(d->size, 0));
setCorner(ToolBox::Top);
} else {
setPos(avail.topRight() - QPoint(d->size, 0));
setCorner(ToolBox::TopRight);
}
}
} else {
if (QApplication::layoutDirection() == Qt::RightToLeft) {
setPos(d->containment->mapFromScene(QPointF(d->containment->geometry().topLeft())));
setCorner(ToolBox::TopLeft);
} else {
setPos(d->containment->mapFromScene(QPointF(d->containment->geometry().topRight())));
setCorner(ToolBox::TopRight);
}
}
}
}
} // plasma namespace
#include "toolbox_p.moc"

View File

@ -24,10 +24,12 @@
#include <QGraphicsItem>
#include <QObject>
#include "animator.h"
#include "containment.h"
class QAction;
class KConfigGroup;
namespace Plasma
{
@ -54,7 +56,7 @@ public:
BottomLeft
};
explicit ToolBox(QGraphicsItem *parent = 0);
explicit ToolBox(Containment *parent);
~ToolBox();
/**
@ -78,6 +80,10 @@ public:
bool isMovable() const;
void setIsMovable(bool movable);
void save(KConfigGroup &cg) const;
void load();
void reposition();
virtual void showToolBox() = 0;
virtual void hideToolBox() = 0;
public Q_SLOTS: