Warning, file /office/calligra/libs/widgets/KoZoomInput.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* Copyright 2008 Peter Simonsson <peter.simonsson@gmail.com> 0002 * 0003 * This library is free software; you can redistribute it and/or 0004 * modify it under the terms of the GNU Library General Public 0005 * License as published by the Free Software Foundation; either 0006 * version 2 of the License, or (at your option) any later version. 0007 * 0008 * This library is distributed in the hope that it will be useful, 0009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 * Library General Public License for more details. 0012 * 0013 * You should have received a copy of the GNU Library General Public License 0014 * along with this library; see the file COPYING.LIB. If not, write to 0015 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0016 * Boston, MA 02110-1301, USA. 0017 */ 0018 0019 #include "KoZoomInput.h" 0020 0021 #include <WidgetsDebug.h> 0022 #include <klocalizedstring.h> 0023 0024 #include <QComboBox> 0025 #include <QLabel> 0026 #include <QHBoxLayout> 0027 #include <QStyle> 0028 #include <QStyleOption> 0029 #include <QPainter> 0030 #include <QPalette> 0031 #include <QAbstractItemView> 0032 #include <QEvent> 0033 #include <QKeyEvent> 0034 #include <QLineEdit> 0035 0036 class KoZoomInput::Private 0037 { 0038 public: 0039 QComboBox* combo; 0040 QLabel* label; 0041 bool inside; 0042 }; 0043 0044 KoZoomInput::KoZoomInput(QWidget* parent) 0045 : QStackedWidget(parent), d(new Private) 0046 { 0047 #ifdef Q_OS_MAC 0048 setAttribute(Qt::WA_MacMiniSize, true); 0049 #endif 0050 0051 QWidget* first = new QWidget(this); 0052 QHBoxLayout* layout = new QHBoxLayout(first); 0053 layout->setSpacing(0); 0054 layout->setMargin(0); 0055 d->label = new QLabel(first); 0056 d->label->setAlignment(Qt::AlignRight | Qt::AlignVCenter); 0057 layout->addWidget(d->label, 10); 0058 QLabel* icon = new QLabel(first); 0059 QStyleOption option; 0060 option.state = QStyle::State_Enabled; 0061 QPixmap pixmap(16, 16); 0062 pixmap.fill(QColor(255, 255, 255, 0)); 0063 QPainter painter(&pixmap); 0064 painter.translate(8, 8); 0065 style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &option, &painter); 0066 icon->setPixmap(pixmap); 0067 layout->addWidget(icon); 0068 addWidget(first); 0069 d->combo = new QComboBox(this); 0070 d->combo->setMaxVisibleItems(15); 0071 d->combo->setEditable(true); 0072 d->combo->installEventFilter(this); 0073 addWidget(d->combo); 0074 d->inside = false; 0075 0076 connect(d->combo, SIGNAL(activated(QString)), this, SIGNAL(zoomLevelChanged(QString))); 0077 } 0078 0079 KoZoomInput::~KoZoomInput() 0080 { 0081 delete d; 0082 } 0083 0084 void KoZoomInput::enterEvent(QEvent* event) 0085 { 0086 Q_UNUSED(event); 0087 0088 d->inside = true; 0089 if (d->combo->view()) 0090 { 0091 d->combo->view()->removeEventFilter(this); 0092 } 0093 0094 setCurrentIndex(1); 0095 } 0096 0097 void KoZoomInput::leaveEvent(QEvent* event) 0098 { 0099 Q_UNUSED(event); 0100 0101 d->inside = false; 0102 if(d->combo->view() && d->combo->view()->isVisible()) 0103 { 0104 d->combo->view()->installEventFilter(this); 0105 return; 0106 } 0107 0108 if(!d->combo->hasFocus()) 0109 setCurrentIndex(0); 0110 } 0111 0112 void KoZoomInput::keyPressEvent(QKeyEvent* event) 0113 { 0114 if (event->key() == Qt::Key_Return || 0115 event->key() == Qt::Key_Enter) 0116 { 0117 focusNextChild(); 0118 } 0119 } 0120 0121 void KoZoomInput::setZoomLevels(const QStringList& levels) 0122 { 0123 d->combo->clear(); 0124 d->combo->addItems(levels); 0125 } 0126 0127 void KoZoomInput::setCurrentZoomLevel(const QString& level) 0128 { 0129 d->combo->setCurrentIndex(d->combo->findText(level)); 0130 d->label->setText(level); 0131 } 0132 0133 bool KoZoomInput::eventFilter(QObject* watched, QEvent* event) 0134 { 0135 if(watched == d->combo->view() && event->type() == QEvent::Hide) 0136 { 0137 focusNextChild(); 0138 setCurrentIndex(0); 0139 } 0140 else if (watched == d->combo && event->type() == QEvent::FocusOut && 0141 (d->combo->view() && !d->combo->view()->hasFocus()) && !d->inside) 0142 { 0143 setCurrentIndex(0); 0144 } 0145 return false; 0146 }