not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / randr / outputgraphicsitem.cpp
blobeb759b054e6c7bc2aac9d0887c2b4869afbe3b14
1 /*
2 * Copyright (c) 2007 Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
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.
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 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.
19 #include "outputgraphicsitem.h"
20 #include "randroutput.h"
21 #include "randr.h"
23 #include <QPen>
24 #include <QBrush>
25 #include <QFont>
26 #include <QGraphicsScene>
27 #include <KGlobalSettings>
29 OutputGraphicsItem::OutputGraphicsItem(RandROutput *output)
30 : QGraphicsRectItem(output->rect())
32 m_left = m_right = m_top = m_bottom = NULL;
34 setPen(QPen(Qt::black));
35 if(output->isActive())
36 setBrush(QColor(0, 255, 0, 128));
37 else setBrush(QColor(128, 128, 128, 0));
39 setFlag(QGraphicsItem::ItemIsMovable, false);
40 setFlag(QGraphicsItem::ItemIsSelectable, true);
42 // An example of this description text with radeonhd on randr 1.2:
43 // DVI-I_2/digital
44 // 1680x1050 (60.0 Hz)
45 int w = output->rect().width(), h = output->rect().height();
46 QString refresh = QString::number(output->refreshRate(), 'f', 1);
47 QString desc = output->name() + '\n' +
48 QString("%1x%2 (%3 Hz)").arg(w).arg(h).arg(refresh);
50 m_text = new QGraphicsTextItem(desc, this);
52 QFont font = KGlobalSettings::generalFont();
53 font.setPixelSize(72);
54 m_text->setFont(font);
56 QRectF textRect = m_text->boundingRect();
57 QRect outRect = output->rect();
59 // more accurate text centering
60 m_text->setPos((outRect.width() - textRect.width()) / 2,
61 (outRect.height() - textRect.height()) / 2);
64 OutputGraphicsItem::~OutputGraphicsItem()
66 disconnect();
69 OutputGraphicsItem *OutputGraphicsItem::left() const
71 return m_left;
74 OutputGraphicsItem *OutputGraphicsItem::right() const
76 return m_right;
79 OutputGraphicsItem *OutputGraphicsItem::top() const
81 return m_top;
84 OutputGraphicsItem *OutputGraphicsItem::bottom() const
86 return m_bottom;
89 void OutputGraphicsItem::setTop(OutputGraphicsItem *output)
91 // if we already have that output at top, then just return
92 if (m_top == output)
93 return;
95 OutputGraphicsItem *oldTop = m_top;
96 m_top = output;
98 // if we currently have a top item, set the given item as the bottom of our old item
99 if (oldTop)
100 oldTop->setBottom(output);
102 // check whether we have a left->top or a right->top to update the pointers
103 if (m_left && m_left->top())
105 OutputGraphicsItem *item = m_left->top();
106 if (item->right())
107 qDebug("Oops, this should not happen");
108 item->setRight(output);
109 if (output)
110 output->setLeft(item);
113 if (m_right && m_right->top())
115 OutputGraphicsItem *item = m_right->top();
116 if (item->left())
117 qDebug("Oops, this should not happen");
118 item->setLeft(output);
119 if (output)
120 output->setRight(item);
124 void OutputGraphicsItem::setBottom(OutputGraphicsItem *output)
126 // if we already have that output at bottom, just return
127 if (m_bottom == output)
128 return;
130 OutputGraphicsItem *oldBottom = m_bottom;
131 m_bottom = output;
133 // if we currently have a bottom item, set the given item as the top of our old item
134 if (oldBottom)
135 oldBottom->setTop(output);
137 // check whether we have a left->bottom or a right->bottom to update the pointers
138 if (m_left && m_left->bottom())
140 OutputGraphicsItem *item = m_left->bottom();
141 if (item->right())
142 qDebug("Oops, this should not happen");
143 item->setRight(output);
144 if (output)
145 output->setLeft(item);
148 if (m_right && m_right->bottom())
150 OutputGraphicsItem *item = m_right->bottom();
151 if (item->left())
152 qDebug("Oops, this should not happen");
153 item->setLeft(output);
154 if (output)
155 output->setRight(item);
159 void OutputGraphicsItem::setLeft(OutputGraphicsItem *output)
161 // if we already have that output at left, then just return
162 if (m_left == output)
163 return;
165 OutputGraphicsItem *oldLeft = m_left;
166 m_left = output;
168 // if we currently have a left item, set the given item as the right of our old item
169 if (oldLeft)
170 oldLeft->setRight(output);
172 // check whether we have a top->left or a bottom->left to update the pointers
173 if (m_top && m_top->left())
175 OutputGraphicsItem *item = m_top->left();
176 if (item->bottom())
177 qDebug("Oops, this should not happen");
178 item->setBottom(output);
179 if (output)
180 output->setTop(item);
183 if (m_bottom && m_bottom->left())
185 OutputGraphicsItem *item = m_bottom->left();
186 if (item->top())
187 qDebug("Oops, this should not happen");
188 item->setTop(output);
189 if (output)
190 output->setBottom(item);
194 void OutputGraphicsItem::setRight(OutputGraphicsItem *output)
196 // if we already have that output at right, then just return
197 if (m_right == output)
198 return;
200 OutputGraphicsItem *oldRight = m_right;
201 m_right = output;
203 // if we currently have a right item, set the given item as the left of our old item
204 if (oldRight)
205 oldRight->setLeft(output);
207 // check whether we have a top->right or a bottom->right to update the pointers
208 if (m_top && m_top->right())
210 OutputGraphicsItem *item = m_top->right();
211 if (item->bottom())
212 qDebug("Oops, this should not happen");
213 item->setBottom(output);
214 if (output)
215 output->setTop(item);
218 if (m_bottom && m_bottom->right())
220 OutputGraphicsItem *item = m_bottom->right();
221 if (item->top())
222 qDebug("Oops, this should not happen");
223 item->setTop(output);
224 if (output)
225 output->setBottom(item);
229 void OutputGraphicsItem::disconnect()
231 // for now just disconnect everything
232 if (m_top)
234 m_top->m_bottom = NULL;
235 if (!m_top->isConnected())
236 emit itemChanged(m_top);
238 if (m_bottom)
240 m_bottom->m_top = NULL;
241 if (!m_bottom->isConnected())
242 emit itemChanged(m_bottom);
244 if (m_left)
246 m_left->m_right = NULL;
247 if (!m_left->isConnected())
248 emit itemChanged(m_left);
250 if (m_right)
252 m_right->m_left = NULL;
253 if (!m_right->isConnected())
254 emit itemChanged(m_right);
257 m_top = m_bottom = m_left = m_right = NULL;
260 void OutputGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
262 // disconnect from the current layout
263 disconnect();
265 QGraphicsRectItem::mousePressEvent(event);
268 void OutputGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
270 QGraphicsRectItem::mouseReleaseEvent(event);
271 emit itemChanged(this);
274 bool OutputGraphicsItem::isConnected()
276 return (m_top != NULL || m_bottom != NULL || m_left != NULL || m_right != NULL);
279 #include "outputgraphicsitem.moc"