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

« back to all changes in this revision

Viewing changes to kdm/kcm/positioner.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 (C) 2006 Oswald Buddenhagen <ossi@kde.org>
 
3
 
 
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.
 
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 GNU
 
12
    General Public License for more details.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
#include "positioner.h"
 
21
 
 
22
#include <klocale.h>
 
23
#include <kstandarddirs.h>
 
24
 
 
25
#include <QFrame>
 
26
#include <QLabel>
 
27
#include <QMouseEvent>
 
28
#include <QDesktopWidget>
 
29
#include <QApplication>
 
30
 
 
31
#define TOTAL_WIDTH 200
 
32
#define TOTAL_HEIGHT 186
 
33
#define ACTIVE_X 23
 
34
#define ACTIVE_Y 14
 
35
#define ACTIVE_WIDTH 151
 
36
#define ACTIVE_HEIGHT 115
 
37
#define MARGIN 10
 
38
 
 
39
#define SNAP 10
 
40
#define STEP 5
 
41
 
 
42
static void
 
43
fit(int &p)
 
44
{
 
45
    if (p < SNAP)
 
46
        p = 0;
 
47
    else if (p > 100 - SNAP)
 
48
        p = 100;
 
49
    else if (p > 50 - SNAP / 2 && p < 50 + SNAP / 2)
 
50
        p = 50;
 
51
}
 
52
 
 
53
static void
 
54
step(int &p, int d)
 
55
{
 
56
    if (p < SNAP)
 
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;
 
62
    else {
 
63
        p += d * STEP;
 
64
        fit(p);
 
65
    }
 
66
}
 
67
 
 
68
Positioner::Positioner(QWidget *parent)
 
69
    : ScreenPreviewWidget(parent)
 
70
    , m_x(50)
 
71
    , m_y(50)
 
72
{
 
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);
 
79
    setMaximumWidth(400);
 
80
    QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
81
    sp.setHeightForWidth(true);
 
82
    setSizePolicy(sp);
 
83
    m_frame = new QFrame(this);
 
84
    m_screen = new QWidget(m_frame);
 
85
    m_screen->setAutoFillBackground(true);
 
86
    QPalette pal;
 
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);
 
92
    QPalette pal2;
 
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);
 
97
    QString wts(i18n(
 
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);
 
104
}
 
105
 
 
106
void
 
107
Positioner::setPosition(int x, int y)
 
108
{
 
109
    m_x = x;
 
110
    m_y = y;
 
111
    updateHandle();
 
112
}
 
113
 
 
114
int
 
115
Positioner::heightForWidth(int w) const
 
116
{
 
117
    const int fw = MARGIN * 2;
 
118
    return ((w - fw) * TOTAL_HEIGHT + (TOTAL_WIDTH / 2)) / TOTAL_WIDTH + fw;
 
119
}
 
120
 
 
121
void
 
122
Positioner::updateHandle()
 
123
{
 
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);
 
131
    int di;
 
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);
 
141
}
 
142
 
 
143
void
 
144
Positioner::resizeEvent(QResizeEvent *event)
 
145
{
 
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);
 
150
 
 
151
    m_frame->setGeometry(0, 0, rs.width() + fw, rs.height() + fw);
 
152
    m_frame->resize(size());
 
153
    m_screen->setGeometry(previewRect());
 
154
 
 
155
    updateHandle();
 
156
}
 
157
 
 
158
void
 
159
Positioner::focusInEvent(QFocusEvent *)
 
160
{
 
161
    m_frame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
 
162
}
 
163
 
 
164
void
 
165
Positioner::focusOutEvent(QFocusEvent *)
 
166
{
 
167
    m_frame->setFrameStyle(QFrame::NoFrame);
 
168
}
 
169
 
 
170
void
 
171
Positioner::mousePressEvent(QMouseEvent *event)
 
172
{
 
173
    QPoint cp = event->pos() - m_screen->pos();
 
174
    if (m_ptr->geometry().contains(cp))
 
175
        m_delta = m_ptr->geometry().center() - cp;
 
176
    else
 
177
        m_delta.setX(-1);
 
178
}
 
179
 
 
180
void
 
181
Positioner::mouseMoveEvent(QMouseEvent *event)
 
182
{
 
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();
 
187
        fit(m_x);
 
188
        fit(m_y);
 
189
        updateHandle();
 
190
        emit positionChanged();
 
191
    }
 
192
}
 
193
 
 
194
void
 
195
Positioner::keyPressEvent(QKeyEvent *event)
 
196
{
 
197
    switch (event->key()) {
 
198
    case Qt::Key_Home:
 
199
        m_x = m_y = 50;
 
200
        break;
 
201
    case Qt::Key_Left:
 
202
        step(m_x, -1);
 
203
        break;
 
204
    case Qt::Key_Right:
 
205
        step(m_x, 1);
 
206
        break;
 
207
    case Qt::Key_Up:
 
208
        step(m_y, -1);
 
209
        break;
 
210
    case Qt::Key_Down:
 
211
        step(m_y, 1);
 
212
        break;
 
213
    default:
 
214
        event->ignore();
 
215
        return;
 
216
    }
 
217
    updateHandle();
 
218
    emit positionChanged();
 
219
    event->accept();
 
220
}
 
221
 
 
222
#include "positioner.moc"