1 /************************************************************************
2  *									*
3  *  This file is part of Kooka, a scanning/OCR application using	*
4  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.	*
5  *									*
6  *  Copyright (C) 2013-2016 Jonathan Marten <jjm@keelhaul.me.uk>	*
7  *									*
8  *  Kooka is free software; you can redistribute it and/or modify it	*
9  *  under the terms of the GNU Library General Public License as	*
10  *  published by the Free Software Foundation and appearing in the	*
11  *  file COPYING included in the packaging of this file;  either	*
12  *  version 2 of the License, or (at your option) any later version.	*
13  *									*
14  *  As a special exception, permission is given to link this program	*
15  *  with any version of the KADMOS OCR/ICR engine (a product of		*
16  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting	*
17  *  executable without including the source code for KADMOS in the	*
18  *  source distribution.						*
19  *									*
20  *  This program is distributed in the hope that it will be useful,	*
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of	*
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	*
23  *  GNU General Public License for more details.			*
24  *									*
25  *  You should have received a copy of the GNU General Public		*
26  *  License along with this program;  see the file COPYING.  If		*
27  *  not, see <http://www.gnu.org/licenses/>.				*
28  *									*
29  ************************************************************************/
30 
31 #include "statusbarmanager.h"
32 
33 #include <qstatusbar.h>
34 #include <qlabel.h>
35 #include <qstyle.h>
36 
37 #include <klocalizedstring.h>
38 #include <kxmlguiwindow.h>
39 
40 #include "imagecanvas.h"
41 #include "previewer.h"
42 #include "sizeindicator.h"
43 
44 
StatusBarManager(KXmlGuiWindow * mainWindow)45 StatusBarManager::StatusBarManager(KXmlGuiWindow *mainWindow)
46     : QObject(mainWindow)
47 {
48     mStatusBar = mainWindow->statusBar();
49     mMargin = 2*mStatusBar->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
50 
51     // Messages
52     mMessageLabel = new QLabel(i18nc("@info:status", "Ready"));
53     mStatusBar->addWidget(mMessageLabel, 1);
54 
55     // Image dimensions
56     QString s = ImageCanvas::imageInfoString(2000, 2000, 48);
57     mImageDimsLabel = new QLabel(s);
58     mImageDimsLabel->setAlignment(Qt::AlignHCenter);
59     mImageDimsLabel->setToolTip(i18nc("@info:tooltip", "The size of the image being viewed in the gallery"));
60     mImageDimsLabel->setMinimumWidth(mImageDimsLabel->sizeHint().width()+mMargin);
61     mStatusBar->addPermanentWidget(mImageDimsLabel);
62 
63     // Preview dimensions
64     s = Previewer::previewInfoString(500.0, 500.0, 1200, 1200);
65     mPreviewDimsLabel = new QLabel(s);
66     mPreviewDimsLabel->setAlignment(Qt::AlignHCenter);
67     mPreviewDimsLabel->setToolTip(i18nc("@info:tooltip", "The size of the selected area that will be scanned"));
68     mPreviewDimsLabel->setMinimumWidth(mPreviewDimsLabel->sizeHint().width()+mMargin);
69     mStatusBar->addPermanentWidget(mPreviewDimsLabel);
70 
71     // Preview file size
72     mFileSize = new SizeIndicator(nullptr);
73     mFileSize->setMaximumWidth(100);
74     mFileSize->setFrameStyle(QFrame::NoFrame);
75     mFileSize->setToolTip(i18nc("@info:tooltip", "<qt>This is the uncompressed size of the scanned image. "
76                                 "It tries to warn you if you try to produce too big an image by "
77                                 "changing its background color."));
78     mStatusBar->addPermanentWidget(mFileSize);
79 
80     mStatusBar->setSizeGripEnabled(false);
81     mStatusBar->show();
82 }
83 
84 
85 // In this application, the length of some of the status bar strings
86 // (e.g. the image/preview dimensions) can vary greatly.  To avoid the
87 // status bar items annoyingly jumping around when this happens, once one
88 // of those items has reached a certain size it is not allowed to shrink
89 // again to less than that.
setLabelText(QLabel * l,const QString & text)90 void StatusBarManager::setLabelText(QLabel *l, const QString &text)
91 {
92     const int oldWidth = l->width();
93     l->setText(text);
94     const int newWidth = l->sizeHint().width();
95     if (newWidth>oldWidth) l->setMinimumWidth(newWidth+mMargin);
96 }
97 
98 
setStatus(const QString & text,StatusBarManager::Item item)99 void StatusBarManager::setStatus(const QString &text, StatusBarManager::Item item)
100 {
101     switch (item)
102     {
103 case StatusBarManager::Message:
104         mMessageLabel->setText(text);
105         break;
106 
107 case StatusBarManager::ImageDims:
108         setLabelText(mImageDimsLabel, i18nc("@info:status", "Image: %1", text));
109         break;
110 
111 case StatusBarManager::PreviewDims:
112         setLabelText(mPreviewDimsLabel, i18nc("@info:status", "Scan: %1", text));
113         break;
114 
115 default:
116         break;
117     }
118 }
119 
120 
clearStatus(StatusBarManager::Item item)121 void StatusBarManager::clearStatus(StatusBarManager::Item item)
122 {
123     setStatus(QString(), item);
124 }
125 
126 
setFileSize(long size)127 void StatusBarManager::setFileSize(long size)
128 {
129     mFileSize->setSizeInByte(size);
130 }
131