2
Copyright (C) 2006 Oswald Buddenhagen <ossi@kde.org>
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, 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 GNU
12
General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#include "positioner.h"
23
#include <kstandarddirs.h>
27
#include <QMouseEvent>
28
#include <QDesktopWidget>
29
#include <QApplication>
31
#define TOTAL_WIDTH 200
32
#define TOTAL_HEIGHT 186
35
#define ACTIVE_WIDTH 151
36
#define ACTIVE_HEIGHT 115
47
else if (p > 100 - SNAP)
49
else if (p > 50 - SNAP / 2 && p < 50 + SNAP / 2)
57
p = 0 + (d + 1) * SNAP / 2;
58
else if (p > 100 - SNAP)
59
p = 100 + (d - 1) * SNAP / 2;
60
else if (p > 50 - SNAP / 2 && p < 50 + SNAP / 2)
61
p = 50 + d * SNAP / 2;
68
Positioner::Positioner(QWidget *parent)
69
: ScreenPreviewWidget(parent)
73
QDesktopWidget *desktop = QApplication::desktop();
74
setRatio((qreal)desktop->width() / (qreal)desktop->height());
75
m_anchor = QPixmap(KStandardDirs::locate("data", "kcontrol/pics/anchor.png"));
76
setFocusPolicy(Qt::StrongFocus);
77
const int fw = MARGIN * 2;
78
setMinimumSize(TOTAL_WIDTH + fw, TOTAL_HEIGHT + fw);
80
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Expanding);
81
sp.setHeightForWidth(true);
83
m_frame = new QFrame(this);
84
m_screen = new QWidget(m_frame);
85
m_screen->setAutoFillBackground(true);
87
pal.setColor(QPalette::Background, QColor(255, 255, 255, 128));
88
m_screen->setPalette(pal);
89
m_dlg = new QFrame(m_screen);
90
m_dlg->setFrameStyle(QFrame::Panel | QFrame::Raised);
91
m_dlg->setAutoFillBackground(true);
93
pal2.setBrush(QPalette::Background, pal2.brush(QPalette::Normal, QPalette::Background));
94
m_dlg->setPalette(pal2);
95
m_ptr = new QLabel(m_screen);
96
m_ptr->setPixmap(m_anchor);
98
"Drag the anchor to move the center of the dialog to the desired position. "
99
"Keyboard control is possible as well: Use the arrow keys or Home to center. "
100
"Note that the actual proportions of the dialog are probably different."));
101
m_frame->setWhatsThis(wts);
102
m_screen->setWhatsThis(wts);
103
m_ptr->setWhatsThis(wts);
107
Positioner::setPosition(int x, int y)
115
Positioner::heightForWidth(int w) const
117
const int fw = MARGIN * 2;
118
return ((w - fw) * TOTAL_HEIGHT + (TOTAL_WIDTH / 2)) / TOTAL_WIDTH + fw;
122
Positioner::updateHandle()
124
int px = m_screen->size().width() * m_x / 100;
125
int py = m_screen->size().height() * m_y / 100;
126
m_ptr->setGeometry(px - m_anchor.width() / 2, py - m_anchor.height() / 2,
127
m_anchor.width(), m_anchor.height());
128
int sw = m_screen->width() * 2 / 5;
129
int sh = m_screen->height() * 2 / 5;
130
QRect grt(px - sw / 2, py - sh / 2, sw, sh);
132
if ((di = m_screen->size().width() - grt.right()) < 0)
133
grt.translate(di, 0);
134
if ((di = - grt.left()) > 0)
135
grt.translate(di, 0);
136
if ((di = m_screen->size().height() - grt.bottom()) < 0)
137
grt.translate(0, di);
138
if ((di = - grt.top()) > 0)
139
grt.translate(0, di);
140
m_dlg->setGeometry(grt);
144
Positioner::resizeEvent(QResizeEvent *event)
146
ScreenPreviewWidget::resizeEvent(event);
147
const int fw = MARGIN * 2;
148
QSize rs(TOTAL_WIDTH, TOTAL_HEIGHT);
149
rs.scale(size() - QSize(fw, fw), Qt::KeepAspectRatio);
151
m_frame->setGeometry(0, 0, rs.width() + fw, rs.height() + fw);
152
m_frame->resize(size());
153
m_screen->setGeometry(previewRect());
159
Positioner::focusInEvent(QFocusEvent *)
161
m_frame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
165
Positioner::focusOutEvent(QFocusEvent *)
167
m_frame->setFrameStyle(QFrame::NoFrame);
171
Positioner::mousePressEvent(QMouseEvent *event)
173
QPoint cp = event->pos() - m_screen->pos();
174
if (m_ptr->geometry().contains(cp))
175
m_delta = m_ptr->geometry().center() - cp;
181
Positioner::mouseMoveEvent(QMouseEvent *event)
183
if (m_delta.x() != -1) {
184
QPoint cp = event->pos() - m_screen->pos() + m_delta;
185
m_x = cp.x() * 100 / m_screen->size().width();
186
m_y = cp.y() * 100 / m_screen->size().height();
190
emit positionChanged();
195
Positioner::keyPressEvent(QKeyEvent *event)
197
switch (event->key()) {
218
emit positionChanged();
222
#include "positioner.moc"