1 /***************************************************************************
2  *   Copyright (C) 2003 by Sébastien Laoût                                 *
3  *   slaout@linux62.org                                                    *
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  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 
21 #include "basketstatusbar.h"
22 
23 #include <QLocale>
24 #include <QStatusBar>
25 
26 #include <QtCore/QObject>
27 #include <QLabel>
28 #include <QtGui/QPixmap>
29 #include <QtGui/QMouseEvent>
30 
31 #include <KParts/StatusBarExtension>
32 #include <KLocalizedString>
33 #include <KIconLoader>
34 
35 #include "global.h"
36 #include "bnpview.h"
37 #include "basketscene.h"
38 #include "tools.h"
39 
BasketStatusBar(QStatusBar * bar)40 BasketStatusBar::BasketStatusBar(QStatusBar *bar)
41         : m_bar(bar), m_extension(0), m_selectionStatus(0), m_lockStatus(0), m_basketStatus(0), m_savedStatus(0)
42 {
43 }
44 
BasketStatusBar(KParts::StatusBarExtension * extension)45 BasketStatusBar::BasketStatusBar(KParts::StatusBarExtension *extension)
46         : m_bar(0), m_extension(extension), m_selectionStatus(0), m_lockStatus(0), m_basketStatus(0), m_savedStatus(0)
47 {
48 }
49 
~BasketStatusBar()50 BasketStatusBar::~BasketStatusBar()
51 {
52     //delete m_extension;
53 }
54 
statusBar() const55 QStatusBar *BasketStatusBar::statusBar() const
56 {
57     if (m_extension)
58         return m_extension->statusBar();
59     else
60         return m_bar;
61 }
62 
addWidget(QWidget * widget,int stretch,bool permanent)63 void BasketStatusBar::addWidget(QWidget * widget, int stretch, bool permanent)
64 {
65     if (m_extension)
66         m_extension->addStatusBarItem(widget, stretch, permanent);
67     else if (permanent)
68         m_bar->addPermanentWidget(widget, stretch);
69     else
70         m_bar->addWidget(widget, stretch);
71 }
72 
setupStatusBar()73 void BasketStatusBar::setupStatusBar()
74 {
75     QWidget* parent = statusBar();
76     QObjectList lst = parent->findChildren<QObject*>("KRSqueezedTextLabel");
77 
78     //Tools::printChildren(parent);
79     if (lst.count() == 0) {
80         m_basketStatus = new QLabel(parent);
81         QSizePolicy policy(QSizePolicy::Ignored, QSizePolicy::Ignored);
82         policy.setHorizontalStretch(0);
83         policy.setVerticalStretch(0);
84         policy.setHeightForWidth(false);
85         m_basketStatus->setSizePolicy(policy);
86         addWidget(m_basketStatus, 1, false);  // Fit all extra space and is hiddable
87     } else
88         m_basketStatus = static_cast<QLabel*>(lst.at(0));
89     lst.clear();
90 
91     m_selectionStatus = new QLabel(i18n("Loading..."), parent);
92     addWidget(m_selectionStatus, 0, true);
93 
94     m_lockStatus = new QLabel(0/*this*/);
95     m_lockStatus->setMinimumSize(18, 18);
96     m_lockStatus->setAlignment(Qt::AlignCenter);
97 //  addWidget( m_lockStatus, 0, true );
98     m_lockStatus->installEventFilter(this);
99 
100     m_savedStatusPixmap = SmallIcon("document-save");
101     m_savedStatus = new QLabel(parent);
102     m_savedStatus->setPixmap(m_savedStatusPixmap);
103     m_savedStatus->setFixedSize(m_savedStatus->sizeHint());
104     m_savedStatus->clear();
105     //m_savedStatus->setPixmap(m_savedStatusIconSet.pixmap(QIconSet::Small, QIconSet::Disabled));
106     //m_savedStatus->setEnabled(false);
107     addWidget(m_savedStatus, 0, true);
108     m_savedStatus->setToolTip("<p>" + i18n("Shows if there are changes that have not yet been saved."));
109 
110 
111 }
112 
postStatusbarMessage(const QString & text)113 void BasketStatusBar::postStatusbarMessage(const QString& text)
114 {
115     if (statusBar())
116         statusBar()->showMessage(text, 2000);
117 }
118 
setStatusText(const QString & txt)119 void BasketStatusBar::setStatusText(const QString &txt)
120 {
121     if (m_basketStatus && m_basketStatus->text() != txt)
122         m_basketStatus->setText(txt);
123 }
124 
setStatusBarHint(const QString & hint)125 void BasketStatusBar::setStatusBarHint(const QString &hint)
126 {
127     if (hint.isEmpty())
128         updateStatusBarHint();
129     else
130         setStatusText(hint);
131 }
132 
updateStatusBarHint()133 void BasketStatusBar::updateStatusBarHint()
134 {
135     QString message = "";
136 
137     if (Global::bnpView->currentBasket()->isDuringDrag())
138         message = i18n("Ctrl+drop: copy, Shift+drop: move, Shift+Ctrl+drop: link.");
139 // Too much noise information:
140 //  else if (currentBasket()->inserterShown() && currentBasket()->inserterSplit() && !currentBasket()->inserterGroup())
141 //      message = i18n("Click to insert a note, right click for more options. Click on the right of the line to group instead of insert.");
142 //  else if (currentBasket()->inserterShown() && currentBasket()->inserterSplit() && currentBasket()->inserterGroup())
143 //      message = i18n("Click to group a note, right click for more options. Click on the left of the line to group instead of insert.");
144     else if (Global::debugWindow)
145         message = "DEBUG: " + Global::bnpView->currentBasket()->folderName();
146 
147     setStatusText(message);
148 }
149 
setLockStatus(bool isLocked)150 void BasketStatusBar::setLockStatus(bool isLocked)
151 {
152     if (!m_lockStatus)
153         return;
154 
155     if (isLocked) {
156         m_lockStatus->setPixmap(SmallIcon("encrypted.png"));
157         m_lockStatus->setToolTip(i18n(
158                                      "<p>This basket is <b>locked</b>.<br>Click to unlock it.</p>").replace(" ", "&nbsp;"));
159     } else {
160         m_lockStatus->clear();
161         m_lockStatus->setToolTip(i18n(
162                                      "<p>This basket is <b>unlocked</b>.<br>Click to lock it.</p>").replace(" ", "&nbsp;"));
163     }
164 }
165 
setSelectionStatus(const QString & s)166 void BasketStatusBar::setSelectionStatus(const QString &s)
167 {
168     if (m_selectionStatus)
169         m_selectionStatus->setText(s);
170 }
171 
setUnsavedStatus(bool isUnsaved)172 void BasketStatusBar::setUnsavedStatus(bool isUnsaved)
173 {
174     if (!m_savedStatus)
175         return;
176 
177     if (isUnsaved) {
178         if (m_savedStatus->pixmap() == 0)
179             m_savedStatus->setPixmap(m_savedStatusPixmap);
180     } else
181         m_savedStatus->clear();
182 }
183 
eventFilter(QObject * obj,QEvent * event)184 bool BasketStatusBar::eventFilter(QObject * obj, QEvent * event)
185 {
186     if (obj == m_lockStatus && event->type() == QEvent::MouseButtonPress) {
187         QMouseEvent * mevent = dynamic_cast<QMouseEvent *>(event);
188         if (mevent->button() & Qt::LeftButton) {
189             Global::bnpView->lockBasket();
190             return true;
191         } else {
192             return QObject::eventFilter(obj, event); // standard event processing
193         }
194     }
195     return QObject::eventFilter(obj, event); // standard event processing
196 }
197 
198