OSDN Git Service

Rename boardmanager.cpp and boardmanager.h
[kita/kita.git] / kita / src / respopup.cpp
1 /***************************************************************************
2 *   Copyright (C) 2007 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 ***************************************************************************/
10
11 #include "respopup.h"
12
13 #include <QtGui/QApplication>
14 #include <QtGui/QCursor>
15 #include <QtGui/QDesktopWidget>
16
17 #include <dom/html_document.h>
18 #include <dom/html_element.h>
19
20 #include "const.h"
21 #include "htmlpart.h"
22 #include "kitaui/htmlview.h"
23 #include "libkita/colorconfig.h"
24 #include "libkita/globalconfig.h"
25
26 using namespace Kita;
27
28 ResPopup::ResPopup(KHTMLView* view, const KUrl& url)
29         : QFrame(view,
30                   Qt::FramelessWindowHint
31                   | Qt::Tool
32                   | Qt::Window
33                   | Qt::X11BypassWindowManagerHint
34                )
35 {
36     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
37     m_url = url;
38     m_htmlPart = 0;
39
40     m_htmlPart = new HTMLPart(this);
41     m_htmlPart->view()->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
42     m_htmlPart->setup(HTMLPART_MODE_POPUP , url);
43     connect(m_htmlPart, SIGNAL(hideChildPopup()), SIGNAL(hideChildPopup()));
44 }
45
46
47 ResPopup::~ResPopup()
48 {
49     delete m_htmlPart;
50 }
51
52
53
54 /* public */
55 void ResPopup::setText(const QString& str)
56 {
57     const int maxwd = 1600;
58     const int maxht = 1200;
59
60     QString style = QString("body.pop {"
61                              " font-size: %1pt; "
62                              " font-family: %2; "
63                              " color: %3; "
64                              " background-color: %4; "
65                              " border-width: 0;"
66                              "}")
67                     .arg(GlobalConfig::popupFont().pointSize())
68                     .arg(GlobalConfig::popupFont().family())
69                     .arg(ColorConfig::popup().name())
70                     .arg(ColorConfig::popupBackground().name());
71
72     QString text = "<html><head><style>";
73     text += DEFAULT_STYLESHEET;
74     text += style;
75     text += "</style></head><body class=\"pop\">";
76     text += str;
77     text += "</body></html>";
78
79     if (m_htmlPart) {
80         m_htmlPart->view()->setMinimumSize(maxwd, maxht);
81         m_htmlPart->setJScriptEnabled(false);
82         m_htmlPart->setJavaEnabled(false);
83         m_htmlPart->begin(KUrl("file:/dummy.htm"));
84         m_htmlPart->write(text);
85         m_htmlPart->end();
86         //m_htmlPart->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); TODO
87     }
88 }
89
90 /* public */
91 void ResPopup::adjustSize()
92 {
93 #if 0
94     if (!m_htmlPart) return ;
95
96     int width = 0, xx = 0, leftmrg = 0;
97     int maxwidth = 0, maxheight = 0;
98     DOM::Node curnode = m_htmlPart->htmlDocument().body().firstChild();
99
100     for (;;) {
101
102         QRect qr = curnode.getRect();
103         int tmpwd = qr.right() - qr.left();
104
105         /*----------------------------------*/
106
107         if (curnode.nodeType() == DOM::Node::TEXT_NODE) {
108             if (xx == 0) xx = qr.left();
109             width += tmpwd;
110         }
111
112         /*----------------------------------*/
113
114         else if (curnode.nodeName().string() == "div") {
115             if (leftmrg == 0) leftmrg = qr.left();
116             width = 0;
117             xx = 0;
118         }
119
120         /*----------------------------------*/
121
122         else if (curnode.nodeName().string() == "br") {
123             width = 0;
124             xx = 0;
125         }
126
127
128         /*----------------------------------*/
129
130         if (leftmrg + xx + width > maxwidth) maxwidth = leftmrg + xx + width;
131         if (qr.bottom() > maxheight) maxheight = qr.bottom();
132
133         /* move to the next node */
134         DOM::Node next = curnode.firstChild();
135
136         if (next.isNull()) next = curnode.nextSibling();
137
138         while (!curnode.isNull() && next.isNull()) {
139             curnode = curnode.parentNode();
140             if (!curnode.isNull()) next = curnode.nextSibling();
141         }
142
143         curnode = next;
144
145         if (curnode.isNull()) break;
146     }
147
148     const int mrg = 32;
149
150     int wd = maxwidth + mrg;
151     int ht = maxheight + mrg;
152 #endif
153     QFrame::adjustSize();
154     m_htmlPart->view()->setMinimumSize(width(), height());
155 }
156
157
158 /* public */
159 void ResPopup::adjustPos(const QPoint& point)
160 {
161     QPoint pos = point;
162     enum{
163         POS_LeftUp,
164         POS_RightUp,
165         POS_LeftDown,
166         POS_RightDown
167     };
168
169     /* config */
170
171     const int mrg = 16;
172
173     /*----------------------------*/
174
175     if (!m_htmlPart) return ;
176
177     QRect qr = QApplication::desktop() ->rect();
178     int sw = qr.width(), sh = qr.height();
179     int wd = width(), ht = height();
180     int x = pos.x(), y = pos.y();
181     int idx;
182
183     if ((x + mrg) + wd < sw
184             && (y - mrg) - ht >= 0) idx = POS_RightUp;
185
186     else if ((x - mrg) - wd >= 0
187               && y - (ht + mrg) >= 0) idx = POS_LeftUp;
188
189     else if ((x + mrg) + wd < sw
190               && (y + mrg) + ht < sh) idx = POS_RightDown;
191
192     else if ((x - mrg) - wd >= 0
193               && (y + mrg) + ht < sh) idx = POS_LeftDown;
194
195     else {
196         int area[ 4 ];
197         area[ 0 ] = (sw - x) * y;
198         area[ 1 ] = x * y;
199         area[ 2 ] = (sw - x) * (sh - y);
200         area[ 3 ] = x * (sh - y);
201
202         idx = 0;
203         for (int i = 1; i < 4; ++i) if (area[ i ] > area[ idx ]) idx = i;
204     }
205
206     switch (idx) {
207
208     case POS_RightUp:
209         x = x + mrg;
210         y = (y - mrg) - ht;
211         break;
212
213     case POS_LeftUp:
214         x = (x - mrg) - wd;
215         y = (y - mrg) - ht;
216         break;
217
218     case POS_RightDown:
219         x = x + mrg;
220         y = y + mrg;
221         break;
222
223     case POS_LeftDown:
224         x = (x - mrg) - wd;
225         y = y + mrg;
226         break;
227     }
228
229     if (x < 0) {
230
231         x = ht % 16;
232     }
233     if (x + wd >= sw) {
234
235         x = sw - wd - (ht % 16);
236
237         if (x < 0) {
238             if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
239             x = 0;
240             wd = sw;
241         }
242     }
243
244     if (y < 0) {
245         if (x <= pos.x() && pos.x() < x + wd) {
246             if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
247             ht += y;
248         }
249         y = 0;
250     }
251     if (y + ht >= sh) {
252
253         if (x <= pos.x() && pos.x() < x + wd) {
254             if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
255             ht = sh - y;
256         } else {
257             y = sh - ht;
258
259             if (y < 0) {
260                 if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
261                 y = 0;
262                 ht = sh;
263             }
264         }
265     }
266
267     pos.setX(x);
268     pos.setY(y);
269     move(pos);
270
271     if (m_htmlPart) m_htmlPart->view() ->resize(wd, ht);
272     resize(wd , ht);
273 }
274
275
276 /* move mouse pointer above the popup frame */  /* public */
277 void ResPopup::moveMouseAbove()
278 {
279     /* config */
280
281     const int mrg = 10;
282
283     /*-------------------------------*/
284
285     QPoint pos = QCursor::pos();
286     int cx = pos.x(), cy = pos.y();
287     int px = x();
288     int py = y();
289     int wd = width();
290     int ht = height();
291
292     if (cx <= px) cx = px + mrg;
293     else if (cx >= px + wd) cx = px + wd - mrg;
294
295     if (cy <= py) cy = py + mrg;
296     else if (cy >= py + ht) cy = py + ht - mrg;
297
298     QCursor::setPos(cx, cy);
299 }