• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • libs
  • kasten
  • controllers
  • view
  • zoom
zoomslider.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Kasten Framework, made within the KDE community.
3 
4  Copyright 2008-2009 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "zoomslider.h"
24 
25 // Kasten gui
26 #include <zoomable.h>
27 // Kasten core
28 #include <abstractmodel.h>
29 // KDE
30 #include <KIcon>
31 #include <KLocale>
32 // Qt
33 #include <QtGui/QSlider>
34 #include <QtGui/QToolButton>
35 #include <QtGui/QLayout>
36 #include <QtGui/QApplication>
37 #include <QtGui/QHelpEvent>
38 
39 
40 namespace Kasten2
41 {
42 
43 static const int ZoomSliderWidth = 150;
44 
45 // TODO: look at Dolphin/Krita/KOffice zoom tool
46 
47 // TODO: different zoom strategies: fixed step size, relative step size
48 // where to put this, behind interface? or better into a zoomtool?
49 
50 ZoomSlider::ZoomSlider( QWidget* parent )
51  : QWidget( parent ), mModel( 0 ), mZoomControl( 0 )
52 {
53  mZoomOutButton = new QToolButton( this );
54  mZoomOutButton->setIcon( KIcon( QLatin1String("zoom-out") ) );
55  mZoomOutButton->setAutoRaise( true );
56 
57  mSlider = new QSlider( Qt::Horizontal, this );
58 
59  mZoomInButton = new QToolButton( this );
60  mZoomInButton->setIcon( KIcon( QLatin1String("zoom-in") ) );
61  mZoomInButton->setAutoRaise( true );
62 
63  QHBoxLayout* layout = new QHBoxLayout( this );
64  layout->setSpacing( 0 );
65  layout->setMargin( 0 );
66  layout->addWidget( mZoomOutButton );
67  layout->addWidget( mSlider );
68  layout->addWidget( mZoomInButton );
69 
70  connect( mZoomOutButton, SIGNAL(clicked()), SLOT(zoomOut()) );
71  connect( mZoomInButton, SIGNAL(clicked()), SLOT(zoomIn()) );
72  connect( mSlider, SIGNAL(valueChanged(int)), SLOT(onSliderValueChanged(int)) );
73  connect( mSlider, SIGNAL(sliderMoved(int)), SLOT(onSliderMoved(int)) );
74 
75  setFixedWidth( ZoomSliderWidth );
76 
77  setTargetModel( 0 );
78 }
79 
80 
81 void ZoomSlider::setTargetModel( AbstractModel* model )
82 {
83  if( mModel ) mModel->disconnect( this );
84 
85  mModel = model ? model->findBaseModelWithInterface<If::Zoomable*>() : 0;
86  mZoomControl = mModel ? qobject_cast<If::Zoomable *>( mModel ) : 0;
87 
88  const bool hasView = ( mZoomControl != 0 );
89  if( hasView )
90  {
91  mSlider->setSingleStep( 1 ); // mZoomControl->zoomLevelSingleStep()?
92  mSlider->setPageStep( 5 ); // mZoomControl->zoomLevelPageStep()?
93 
94  const int min = 0; //mZoomControl->minimumZoomLevel();
95  const int max = 99; //mZoomControl->maximumZoomLevel();
96  mSlider->setRange( min, max );
97 
98  onZoomLevelChange( mZoomControl->zoomLevel() );
99  const int sliderValue = mSlider->value();
100  mZoomOutButton->setEnabled( sliderValue > mSlider->minimum() );
101  mZoomInButton->setEnabled( sliderValue < mSlider->maximum() );
102  connect( mModel, SIGNAL(zoomLevelChanged(double)), SLOT(onZoomLevelChange(double)) );
103  }
104  else
105  {
106  mZoomOutButton->setEnabled( false );
107  mZoomInButton->setEnabled( false );
108  // put slider in the middle
109  mSlider->setRange( 0, 99 );
110  mSlider->setValue( 50 );
111  }
112 
113  mSlider->setEnabled( hasView );
114 }
115 
116 void ZoomSlider::updateToolTip( int sliderValue )
117 {
118  const float zoomLevel = 50.0 / (100-sliderValue);
119  const int zoomPercent = static_cast<int>( zoomLevel*100 + 0.5 );
120  mSlider->setToolTip( i18nc("@info:tooltip", "Zoom: %1%", zoomPercent) );
121 // TODO: get the text by a signal toolTipNeeded( int zoomLevel, QString* toolTipText ); ?
122 }
123 
124 void ZoomSlider::zoomOut()
125 {
126  const int newValue = mSlider->value() - mSlider->singleStep();
127  mSlider->setValue( newValue );
128 }
129 
130 void ZoomSlider::zoomIn()
131 {
132  const int newValue = mSlider->value() + mSlider->singleStep();
133  mSlider->setValue( newValue );
134 }
135 
136 
137 void ZoomSlider::onSliderValueChanged( int sliderValue )
138 {
139  updateToolTip( sliderValue );
140  mZoomOutButton->setEnabled( sliderValue > mSlider->minimum() );
141  mZoomInButton->setEnabled( sliderValue < mSlider->maximum() );
142 
143  if( mZoomControl )
144  mZoomControl->setZoomLevel( 50.0 / (100-sliderValue) );
145 }
146 
147 // TODO: which signal comes first, valueChanged or sliderMoved?
148 // ensure correct calculation of zoomLevel, best by model
149 // but can be timeconsuming?
150 // use timer to delay resize, so that sliding is not delayed by resizing
151 void ZoomSlider::onSliderMoved( int sliderValue )
152 {
153 Q_UNUSED( sliderValue )
154 
155  QPoint toolTipPoint = mSlider->rect().topLeft();
156  toolTipPoint.ry() += mSlider->height() / 2;
157  toolTipPoint = mSlider->mapToGlobal( toolTipPoint );
158 
159  QHelpEvent toolTipEvent( QEvent::ToolTip, QPoint(0, 0), toolTipPoint );
160  QApplication::sendEvent( mSlider, &toolTipEvent );
161 }
162 
163 void ZoomSlider::onZoomLevelChange( double level )
164 {
165  mZoomLevel = level;
166  const int newSliderValue = 100-static_cast<int>( 50.0 / mZoomLevel + 0.5 );
167  if( newSliderValue != mSlider->value() )
168  {
169  disconnect( mSlider, SIGNAL(valueChanged(int)), this, 0 );
170  mSlider->setSliderPosition( newSliderValue );
171  updateToolTip( mSlider->value() );
172  connect( mSlider, SIGNAL(valueChanged(int)), SLOT(onSliderValueChanged(int)) );
173  }
174 }
175 
176 
177 ZoomSlider::~ZoomSlider()
178 {
179 }
180 
181 }
abstractmodel.h
zoomable.h
Kasten2::ZoomSlider::ZoomSlider
ZoomSlider(QWidget *parent)
Definition: zoomslider.cpp:50
Kasten2::AbstractModel::findBaseModelWithInterface
AbstractModel * findBaseModelWithInterface() const
returns the first baseModel which is of type T, or null if none is found.
Definition: abstractmodel.h:109
QWidget
Kasten2::If::Zoomable::setZoomLevel
virtual void setZoomLevel(double Level)=0
Kasten2::ZoomSliderWidth
static const int ZoomSliderWidth
Definition: zoomslider.cpp:43
Kasten2::If::Zoomable
Definition: zoomable.h:37
Kasten2::ZoomSlider::~ZoomSlider
virtual ~ZoomSlider()
Definition: zoomslider.cpp:177
Kasten2::If::Zoomable::zoomLevel
virtual double zoomLevel() const =0
zoomslider.h
Kasten2::ZoomSlider::setTargetModel
void setTargetModel(AbstractModel *model)
Definition: zoomslider.cpp:81
Kasten2::AbstractModel
Definition: abstractmodel.h:40
QToolButton
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal