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

« back to all changes in this revision

Viewing changes to kcontrol/randr/outputgraphicsitem.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) 2007      Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
 
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 published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (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 General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "outputgraphicsitem.h"
 
20
#include "outputconfig.h"
 
21
#include "randr.h"
 
22
 
 
23
#include <QPen>
 
24
#include <QBrush>
 
25
#include <QFont>
 
26
#include <QGraphicsScene>
 
27
#include <KGlobalSettings>
 
28
 
 
29
OutputGraphicsItem::OutputGraphicsItem(OutputConfig *config)
 
30
        : QGraphicsRectItem(config->rect())
 
31
        , m_config( config )
 
32
{
 
33
        m_left = m_right = m_top = m_bottom = NULL;
 
34
 
 
35
        setPen(QPen(Qt::black));
 
36
 
 
37
        setFlag(QGraphicsItem::ItemIsMovable, false);
 
38
// FIXME not implemented yet    setFlag(QGraphicsItem::ItemIsSelectable, true);
 
39
        
 
40
        m_text = new QGraphicsTextItem(QString(), this);
 
41
        
 
42
        QFont font = KGlobalSettings::generalFont();
 
43
        font.setPixelSize(72);
 
44
        m_text->setFont(font);
 
45
        setVisible( false );
 
46
        m_text->setVisible( false );
 
47
}
 
48
 
 
49
OutputGraphicsItem::~OutputGraphicsItem()
 
50
{
 
51
        disconnect();
 
52
}
 
53
 
 
54
void OutputGraphicsItem::configUpdated()
 
55
{
 
56
        if( !m_config->isActive()) {
 
57
                setVisible( false );
 
58
                m_text->setVisible( false );
 
59
                return;
 
60
        }
 
61
        setVisible( true );
 
62
        m_text->setVisible( true );
 
63
        setRect( m_config->rect());
 
64
        setBrush(QColor(0, 255, 0, 128));
 
65
        // An example of this description text with radeonhd on randr 1.2:
 
66
        // DVI-I_2/digital
 
67
        // 1680x1050 (60.0 Hz)
 
68
        QString refresh = QString::number(m_config->refreshRate(), 'f', 1);
 
69
        QString desc = m_config->output()->name() + '\n' + 
 
70
                       QString("%1x%2 (%3 Hz)").arg(rect().width()).arg(rect().height()).arg(refresh);
 
71
        m_text->setPlainText( desc );
 
72
        // more accurate text centering
 
73
        QRectF textRect = m_text->boundingRect();
 
74
        m_text->setPos( rect().x() + (rect().width() - textRect.width()) / 2,
 
75
                        rect().y() + (rect().height() - textRect.height()) / 2);
 
76
}
 
77
 
 
78
OutputGraphicsItem *OutputGraphicsItem::left() const
 
79
{
 
80
        return m_left;
 
81
}
 
82
 
 
83
OutputGraphicsItem *OutputGraphicsItem::right() const
 
84
{
 
85
        return m_right;
 
86
}
 
87
 
 
88
OutputGraphicsItem *OutputGraphicsItem::top() const
 
89
{
 
90
        return m_top;
 
91
}
 
92
 
 
93
OutputGraphicsItem *OutputGraphicsItem::bottom() const
 
94
{
 
95
        return m_bottom;
 
96
}
 
97
 
 
98
void OutputGraphicsItem::setTop(OutputGraphicsItem *output)
 
99
{
 
100
        // if we already have that output at top, then just return
 
101
        if (m_top == output)
 
102
        return;
 
103
   
 
104
   OutputGraphicsItem *oldTop = m_top;
 
105
   m_top = output;
 
106
 
 
107
   // if we currently have a top item, set the given item as the bottom of our old item
 
108
   if (oldTop)
 
109
           oldTop->setBottom(output);
 
110
 
 
111
   // check whether we have a left->top or a right->top to update the pointers
 
112
   if (m_left && m_left->top())
 
113
   {
 
114
           OutputGraphicsItem *item = m_left->top();
 
115
           if (item->right())
 
116
           qDebug("Oops, this should not happen");
 
117
        item->setRight(output);
 
118
        if (output)
 
119
                output->setLeft(item);
 
120
   }
 
121
 
 
122
   if (m_right && m_right->top())
 
123
   {
 
124
           OutputGraphicsItem *item = m_right->top();
 
125
           if (item->left())
 
126
           qDebug("Oops, this should not happen");
 
127
           item->setLeft(output);
 
128
           if (output)
 
129
                   output->setRight(item);
 
130
   }
 
131
}
 
132
 
 
133
void OutputGraphicsItem::setBottom(OutputGraphicsItem *output)
 
134
{
 
135
        // if we already have that output at bottom, just return
 
136
        if (m_bottom == output)
 
137
        return;
 
138
   
 
139
   OutputGraphicsItem *oldBottom = m_bottom;
 
140
   m_bottom = output;
 
141
 
 
142
   // if we currently have a bottom item, set the given item as the top of our old item
 
143
   if (oldBottom)
 
144
        oldBottom->setTop(output);
 
145
 
 
146
   // check whether we have a left->bottom or a right->bottom to update the pointers
 
147
   if (m_left && m_left->bottom())
 
148
   {
 
149
           OutputGraphicsItem *item = m_left->bottom();
 
150
           if (item->right())
 
151
           qDebug("Oops, this should not happen");
 
152
        item->setRight(output);
 
153
        if (output)
 
154
                output->setLeft(item);
 
155
   }
 
156
 
 
157
   if (m_right && m_right->bottom())
 
158
   {
 
159
           OutputGraphicsItem *item = m_right->bottom();
 
160
           if (item->left())
 
161
           qDebug("Oops, this should not happen");
 
162
           item->setLeft(output);
 
163
           if (output)
 
164
                   output->setRight(item);
 
165
   }
 
166
}
 
167
 
 
168
void OutputGraphicsItem::setLeft(OutputGraphicsItem *output)
 
169
{
 
170
        // if we already have that output at left, then just return
 
171
        if (m_left == output)
 
172
        return;
 
173
   
 
174
   OutputGraphicsItem *oldLeft = m_left;
 
175
   m_left = output;
 
176
 
 
177
   // if we currently have a left item, set the given item as the right of our old item
 
178
   if (oldLeft)
 
179
        oldLeft->setRight(output);
 
180
 
 
181
   // check whether we have a top->left or a bottom->left to update the pointers
 
182
   if (m_top && m_top->left())
 
183
   {
 
184
           OutputGraphicsItem *item = m_top->left();
 
185
           if (item->bottom())
 
186
           qDebug("Oops, this should not happen");
 
187
        item->setBottom(output);
 
188
        if (output)
 
189
                output->setTop(item);
 
190
   }
 
191
 
 
192
   if (m_bottom && m_bottom->left())
 
193
   {
 
194
           OutputGraphicsItem *item = m_bottom->left();
 
195
           if (item->top())
 
196
           qDebug("Oops, this should not happen");
 
197
           item->setTop(output);
 
198
           if (output)
 
199
                   output->setBottom(item);
 
200
   }
 
201
}
 
202
 
 
203
void OutputGraphicsItem::setRight(OutputGraphicsItem *output)
 
204
{
 
205
        // if we already have that output at right, then just return
 
206
        if (m_right == output)
 
207
        return;
 
208
   
 
209
   OutputGraphicsItem *oldRight = m_right;
 
210
   m_right = output;
 
211
 
 
212
   // if we currently have a right item, set the given item as the left of our old item
 
213
   if (oldRight)
 
214
        oldRight->setLeft(output);
 
215
 
 
216
   // check whether we have a top->right or a bottom->right to update the pointers
 
217
   if (m_top && m_top->right())
 
218
   {
 
219
           OutputGraphicsItem *item = m_top->right();
 
220
           if (item->bottom())
 
221
           qDebug("Oops, this should not happen");
 
222
        item->setBottom(output);
 
223
        if (output)
 
224
                output->setTop(item);
 
225
   }
 
226
 
 
227
   if (m_bottom && m_bottom->right())
 
228
   {
 
229
           OutputGraphicsItem *item = m_bottom->right();
 
230
           if (item->top())
 
231
           qDebug("Oops, this should not happen");
 
232
           item->setTop(output);
 
233
           if (output)
 
234
                   output->setBottom(item);
 
235
   }
 
236
}
 
237
 
 
238
void OutputGraphicsItem::disconnect()
 
239
{
 
240
        // for now just disconnect everything
 
241
        if (m_top)
 
242
        {
 
243
                m_top->m_bottom = NULL;
 
244
                if (!m_top->isConnected())
 
245
                        emit itemChanged(m_top);
 
246
        }
 
247
        if (m_bottom)
 
248
        {
 
249
                m_bottom->m_top = NULL;
 
250
                if (!m_bottom->isConnected())
 
251
                        emit itemChanged(m_bottom);
 
252
        }
 
253
        if (m_left)
 
254
        {
 
255
                m_left->m_right = NULL;
 
256
                if (!m_left->isConnected())
 
257
                        emit itemChanged(m_left);
 
258
        }
 
259
        if (m_right)
 
260
        {
 
261
                m_right->m_left = NULL;
 
262
                if (!m_right->isConnected())
 
263
                        emit itemChanged(m_right);
 
264
        }
 
265
 
 
266
        m_top = m_bottom = m_left = m_right = NULL;
 
267
}
 
268
 
 
269
void OutputGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 
270
{
 
271
        // disconnect from the current layout
 
272
        disconnect();
 
273
 
 
274
        QGraphicsRectItem::mousePressEvent(event);
 
275
}
 
276
 
 
277
void OutputGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 
278
{
 
279
   QGraphicsRectItem::mouseReleaseEvent(event);
 
280
   emit itemChanged(this);
 
281
}
 
282
 
 
283
bool OutputGraphicsItem::isConnected()
 
284
{
 
285
        return (m_top != NULL || m_bottom != NULL || m_left != NULL || m_right != NULL);
 
286
}
 
287
 
 
288
#include "outputgraphicsitem.moc"