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"
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:
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()
69 OutputGraphicsItem
*OutputGraphicsItem::left() const
74 OutputGraphicsItem
*OutputGraphicsItem::right() const
79 OutputGraphicsItem
*OutputGraphicsItem::top() const
84 OutputGraphicsItem
*OutputGraphicsItem::bottom() const
89 void OutputGraphicsItem::setTop(OutputGraphicsItem
*output
)
91 // if we already have that output at top, then just return
95 OutputGraphicsItem
*oldTop
= m_top
;
98 // if we currently have a top item, set the given item as the bottom of our old item
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();
107 qDebug("Oops, this should not happen");
108 item
->setRight(output
);
110 output
->setLeft(item
);
113 if (m_right
&& m_right
->top())
115 OutputGraphicsItem
*item
= m_right
->top();
117 qDebug("Oops, this should not happen");
118 item
->setLeft(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
)
130 OutputGraphicsItem
*oldBottom
= m_bottom
;
133 // if we currently have a bottom item, set the given item as the top of our old item
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();
142 qDebug("Oops, this should not happen");
143 item
->setRight(output
);
145 output
->setLeft(item
);
148 if (m_right
&& m_right
->bottom())
150 OutputGraphicsItem
*item
= m_right
->bottom();
152 qDebug("Oops, this should not happen");
153 item
->setLeft(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
)
165 OutputGraphicsItem
*oldLeft
= m_left
;
168 // if we currently have a left item, set the given item as the right of our old item
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();
177 qDebug("Oops, this should not happen");
178 item
->setBottom(output
);
180 output
->setTop(item
);
183 if (m_bottom
&& m_bottom
->left())
185 OutputGraphicsItem
*item
= m_bottom
->left();
187 qDebug("Oops, this should not happen");
188 item
->setTop(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
)
200 OutputGraphicsItem
*oldRight
= m_right
;
203 // if we currently have a right item, set the given item as the left of our old item
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();
212 qDebug("Oops, this should not happen");
213 item
->setBottom(output
);
215 output
->setTop(item
);
218 if (m_bottom
&& m_bottom
->right())
220 OutputGraphicsItem
*item
= m_bottom
->right();
222 qDebug("Oops, this should not happen");
223 item
->setTop(output
);
225 output
->setBottom(item
);
229 void OutputGraphicsItem::disconnect()
231 // for now just disconnect everything
234 m_top
->m_bottom
= NULL
;
235 if (!m_top
->isConnected())
236 emit
itemChanged(m_top
);
240 m_bottom
->m_top
= NULL
;
241 if (!m_bottom
->isConnected())
242 emit
itemChanged(m_bottom
);
246 m_left
->m_right
= NULL
;
247 if (!m_left
->isConnected())
248 emit
itemChanged(m_left
);
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
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"