3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #define DEBUG_STATUS_BAR (DEBUG_KP_MAIN_WINDOW && 0)
31 #include <kpMainWindow.h>
32 #include <kpMainWindowPrivate.h>
39 #include <kstatusbar.h>
42 #include <kpDocument.h>
43 #include <kpSqueezedTextLabel.h>
45 #include <kpViewManager.h>
46 #include <kpViewScrollableContainer.h>
47 #include <kpZoomedView.h>
51 void kpMainWindow::addPermanentStatusBarItem (int id
, int maxTextLen
)
53 KStatusBar
*sb
= statusBar ();
55 QString textWithMaxLen
;
56 textWithMaxLen
.fill (QString::number (8/*big fat*/).at (0),
57 maxTextLen
); //+ 2/*spaces on either side*/);
59 // Permanent --> place on the right
60 sb
->insertPermanentFixedItem (textWithMaxLen
, id
);
61 sb
->changeItem (QString(), id
);
65 void kpMainWindow::createStatusBar ()
67 KStatusBar
*sb
= statusBar ();
69 // 9999 pixels "ought to be enough for anybody"
70 const int maxDimenLength
= 4;
72 //sb->insertItem (QString(), StatusBarItemMessage, 1/*stretch*/);
73 //sb->setItemAlignment (StatusBarItemMessage, Qt::AlignLeft | Qt::AlignVCenter);
75 d
->statusBarMessageLabel
= new kpSqueezedTextLabel (sb
);
76 //d->statusBarMessageLabel->setShowEllipsis (false);
77 sb
->addWidget (d
->statusBarMessageLabel
, 1/*stretch*/);
79 addPermanentStatusBarItem (StatusBarItemShapePoints
,
80 (maxDimenLength
+ 1/*,*/ + maxDimenLength
) * 2 + 3/* - */);
81 addPermanentStatusBarItem (StatusBarItemShapeSize
,
82 (1/*+/-*/ + maxDimenLength
) * 2 + 1/*x*/);
84 // TODO: This doesn't seem to fit "5000x5000".
85 addPermanentStatusBarItem (StatusBarItemDocSize
,
86 maxDimenLength
+ 1/*x*/ + maxDimenLength
);
87 addPermanentStatusBarItem (StatusBarItemDocDepth
,
90 addPermanentStatusBarItem (StatusBarItemZoom
,
93 d
->statusBarShapeLastPointsInitialised
= false;
94 d
->statusBarShapeLastSizeInitialised
= false;
95 d
->statusBarCreated
= true;
100 void kpMainWindow::setStatusBarMessage (const QString
&message
)
102 #if DEBUG_STATUS_BAR && 1
103 kDebug () << "kpMainWindow::setStatusBarMessage("
105 << ") ok=" << d
->statusBarCreated
109 if (!d
->statusBarCreated
)
112 //statusBar ()->changeItem (message, StatusBarItemMessage);
113 d
->statusBarMessageLabel
->setText (message
);
117 void kpMainWindow::setStatusBarShapePoints (const QPoint
&startPoint
,
118 const QPoint
&endPoint
)
120 #if DEBUG_STATUS_BAR && 0
121 kDebug () << "kpMainWindow::setStatusBarShapePoints("
122 << startPoint
<< "," << endPoint
123 << ") ok=" << d
->statusBarCreated
127 if (!d
->statusBarCreated
)
130 if (d
->statusBarShapeLastPointsInitialised
&&
131 startPoint
== d
->statusBarShapeLastStartPoint
&&
132 endPoint
== d
->statusBarShapeLastEndPoint
)
134 #if DEBUG_STATUS_BAR && 0
135 kDebug () << "\tNOP";
140 if (startPoint
== KP_INVALID_POINT
)
142 statusBar ()->changeItem (QString(), StatusBarItemShapePoints
);
144 else if (endPoint
== KP_INVALID_POINT
)
146 statusBar ()->changeItem (i18n ("%1,%2",
149 StatusBarItemShapePoints
);
153 statusBar ()->changeItem (i18n ("%1,%2 - %3,%4",
158 StatusBarItemShapePoints
);
161 d
->statusBarShapeLastStartPoint
= startPoint
;
162 d
->statusBarShapeLastEndPoint
= endPoint
;
163 d
->statusBarShapeLastPointsInitialised
= true;
167 void kpMainWindow::setStatusBarShapeSize (const QSize
&size
)
169 #if DEBUG_STATUS_BAR && 0
170 kDebug () << "kpMainWindow::setStatusBarShapeSize("
172 << ") ok=" << d
->statusBarCreated
176 if (!d
->statusBarCreated
)
179 if (d
->statusBarShapeLastSizeInitialised
&&
180 size
== d
->statusBarShapeLastSize
)
182 #if DEBUG_STATUS_BAR && 0
183 kDebug () << "\tNOP";
188 if (size
== KP_INVALID_SIZE
)
190 statusBar ()->changeItem (QString(), StatusBarItemShapeSize
);
194 statusBar ()->changeItem (i18n ("%1x%2",
197 StatusBarItemShapeSize
);
200 d
->statusBarShapeLastSize
= size
;
201 d
->statusBarShapeLastSizeInitialised
= true;
205 void kpMainWindow::setStatusBarDocSize (const QSize
&size
)
207 #if DEBUG_STATUS_BAR && 0
208 kDebug () << "kpMainWindow::setStatusBarDocSize("
210 << ") ok=" << d
->statusBarCreated
214 if (!d
->statusBarCreated
)
217 if (size
== KP_INVALID_SIZE
)
219 statusBar ()->changeItem (QString(), StatusBarItemDocSize
);
223 statusBar ()->changeItem (i18n ("%1x%2",
226 StatusBarItemDocSize
);
231 void kpMainWindow::setStatusBarDocDepth (int depth
)
233 #if DEBUG_STATUS_BAR && 0
234 kDebug () << "kpMainWindow::setStatusBarDocDepth("
236 << ") ok=" << d
->statusBarCreated
240 if (!d
->statusBarCreated
)
245 statusBar ()->changeItem (QString(), StatusBarItemDocDepth
);
249 statusBar ()->changeItem (i18n ("%1bpp", depth
),
250 StatusBarItemDocDepth
);
255 void kpMainWindow::setStatusBarZoom (int zoom
)
257 #if DEBUG_STATUS_BAR && 0
258 kDebug () << "kpMainWindow::setStatusBarZoom("
260 << ") ok=" << d
->statusBarCreated
264 if (!d
->statusBarCreated
)
269 statusBar ()->changeItem (QString(), StatusBarItemZoom
);
273 statusBar ()->changeItem (i18n ("%1%", zoom
),
278 void kpMainWindow::recalculateStatusBarMessage ()
280 #if DEBUG_STATUS_BAR && 1
281 kDebug () << "kpMainWindow::recalculateStatusBarMessage()";
283 QString scrollViewMessage
= d
->scrollView
->statusMessage ();
284 #if DEBUG_STATUS_BAR && 1
285 kDebug () << "\tscrollViewMessage=" << scrollViewMessage
;
286 kDebug () << "\tresizing doc? " << !d
->scrollView
->newDocSize ().isEmpty ()
288 kDebug () << "\tviewUnderCursor? "
289 << (d
->viewManager
&& d
->viewManager
->viewUnderCursor ())
293 // HACK: To work around kpViewScrollableContainer's unreliable
294 // status messages (which in turn is due to Qt not updating
295 // QWidget::underMouse() on drags and we needing to hack around it)
296 if (!scrollViewMessage
.isEmpty () &&
297 d
->scrollView
->newDocSize ().isEmpty () &&
298 d
->viewManager
&& d
->viewManager
->viewUnderCursor ())
300 #if DEBUG_STATUS_BAR && 1
301 kDebug () << "\t\tnot resizing & viewUnderCursor - message is wrong - clearing"
304 d
->scrollView
->blockSignals (true);
305 d
->scrollView
->clearStatusMessage ();
306 d
->scrollView
->blockSignals (false);
308 scrollViewMessage
.clear ();
309 #if DEBUG_STATUS_BAR && 1
310 kDebug () << "\t\t\tdone";
314 if (!scrollViewMessage
.isEmpty ())
316 setStatusBarMessage (scrollViewMessage
);
320 const kpTool
*t
= tool ();
323 setStatusBarMessage (t
->userMessage ());
327 setStatusBarMessage ();
333 void kpMainWindow::recalculateStatusBarShape ()
335 #if DEBUG_STATUS_BAR && 0
336 kDebug () << "kpMainWindow::recalculateStatusBarShape()";
339 QSize docResizeTo
= d
->scrollView
->newDocSize ();
340 #if DEBUG_STATUS_BAR && 0
341 kDebug () << "\tdocResizeTo=" << docResizeTo
;
343 if (docResizeTo
.isValid ())
345 const QPoint
startPoint (d
->document
->width (), d
->document
->height ());
346 #if DEBUG_STATUS_BAR && 0
347 kDebug () << "\thavedMovedFromOrgSize="
348 << d
->scrollView
->haveMovedFromOriginalDocSize () << endl
;
350 if (!d
->scrollView
->haveMovedFromOriginalDocSize ())
352 setStatusBarShapePoints (startPoint
);
353 setStatusBarShapeSize ();
357 const int newWidth
= docResizeTo
.width ();
358 const int newHeight
= docResizeTo
.height ();
360 setStatusBarShapePoints (startPoint
, QPoint (newWidth
, newHeight
));
361 const QPoint
sizeAsPoint (QPoint (newWidth
, newHeight
) - startPoint
);
362 setStatusBarShapeSize (QSize (sizeAsPoint
.x (), sizeAsPoint
.y ()));
367 const kpTool
*t
= tool ();
368 #if DEBUG_STATUS_BAR && 0
369 kDebug () << "\ttool=" << t
;
373 setStatusBarShapePoints (t
->userShapeStartPoint (),
374 t
->userShapeEndPoint ());
375 setStatusBarShapeSize (t
->userShapeSize ());
379 setStatusBarShapePoints ();
380 setStatusBarShapeSize ();
386 void kpMainWindow::recalculateStatusBar ()
388 #if DEBUG_STATUS_BAR && 1
389 kDebug () << "kpMainWindow::recalculateStatusBar() ok="
390 << d
->statusBarCreated
394 if (!d
->statusBarCreated
)
397 recalculateStatusBarMessage ();
398 recalculateStatusBarShape ();
402 setStatusBarDocSize (QSize (d
->document
->width (), d
->document
->height ()));
403 setStatusBarDocDepth (d
->document
->image ().depth ());
407 setStatusBarDocSize ();
408 setStatusBarDocDepth ();
413 setStatusBarZoom (d
->mainView
->zoomLevelX ());