• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KPIMTextedit Library

  • kpimtextedit
insertimagewidget.cpp
1 /*
2  Copyright (c) 2012 Montel Laurent <montel@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 
19 */
20 #include "insertimagewidget.h"
21 #include <KLocale>
22 #include <KUrlRequester>
23 #include <KImageIO>
24 #include <KFileDialog>
25 
26 #include <QVBoxLayout>
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QSpinBox>
30 #include <QCheckBox>
31 #include <KLineEdit>
32 
33 
34 
35 namespace KPIMTextEdit
36 {
37 
38 class InsertImageWidgetPrivate
39 {
40 public:
41  InsertImageWidgetPrivate(InsertImageWidget *qq)
42  :imageRatio(-1), q( qq )
43  {
44  QVBoxLayout *lay = new QVBoxLayout( q );
45  QHBoxLayout *hbox = new QHBoxLayout;
46  QLabel *lab = new QLabel( i18n( "Image Location:" ) );
47  imageUrlRequester = new KUrlRequester;
48 
49  const QStringList mimetypes = KImageIO::mimeTypes( KImageIO::Reading );
50  imageUrlRequester->fileDialog()->setFilter( mimetypes.join( QLatin1String( " " ) ) );
51  imageUrlRequester->fileDialog()->setOperationMode( KFileDialog::Other );
52  imageUrlRequester->fileDialog()->setCaption( i18n( "Add Image" ) );
53  imageUrlRequester->fileDialog()->okButton()->setGuiItem( KGuiItem( i18n( "&Add" ), QLatin1String( "document-open" ) ) );
54  imageUrlRequester->fileDialog()->setMode( KFile::File );
55  q->connect( imageUrlRequester->lineEdit(), SIGNAL(textChanged(QString)), q, SLOT(_k_slotUrlChanged(QString)) );
56 
57  hbox->addWidget( lab );
58  hbox->addWidget( imageUrlRequester );
59  lab->setBuddy( imageUrlRequester );
60  lay->addLayout( hbox );
61 
62  keepOriginalSize = new QCheckBox( i18n( "Keep Original Size" ) );
63  q->connect( keepOriginalSize, SIGNAL(clicked(bool)), q, SLOT(_k_slotKeepOriginalSizeClicked(bool)) );
64  keepOriginalSize->setChecked( true );
65  lay->addWidget( keepOriginalSize );
66 
67  keepImageRatio = new QCheckBox( i18n( "Keep Image Ratio" ) );
68  keepImageRatio->setChecked( true );
69  keepImageRatio->setEnabled( false );
70  lay->addWidget( keepImageRatio );
71 
72  hbox = new QHBoxLayout;
73  lab = new QLabel( i18n( "Width:" ) );
74  width = new QSpinBox;
75  width->setMinimum( 1 );
76  width->setMaximum( 99999 );
77  width->setEnabled( false );
78  width->setSuffix( i18n( " px" ) );
79  lab->setBuddy( width );
80  q->connect( width, SIGNAL(valueChanged(int)), q, SLOT(_k_slotImageWidthChanged(int)) );
81  hbox->addWidget( lab );
82  hbox->addWidget( width );
83  lay->addLayout( hbox );
84 
85  hbox = new QHBoxLayout;
86  lab = new QLabel( i18n( "Height:" ) );
87  height = new QSpinBox;
88  height->setMinimum( 1 );
89  height->setMaximum( 99999 );
90  height->setEnabled( false );
91  height->setSuffix( i18n( " px" ) );
92  lab->setBuddy( height );
93  q->connect( height, SIGNAL(valueChanged(int)), q, SLOT(_k_slotImageHeightChanged(int)) );
94  hbox->addWidget( lab );
95  hbox->addWidget( height );
96  lay->addLayout( hbox );
97  }
98 
99  void _k_slotKeepOriginalSizeClicked(bool);
100  void _k_slotUrlChanged(const QString&);
101  void _k_slotImageWidthChanged(int);
102  void _k_slotImageHeightChanged(int);
103 
104  qreal imageRatio;
105  QCheckBox *keepOriginalSize;
106  QCheckBox *keepImageRatio;
107  QSpinBox *width;
108  QSpinBox *height;
109  KUrlRequester *imageUrlRequester;
110  InsertImageWidget *q;
111 };
112 
113 void InsertImageWidgetPrivate::_k_slotKeepOriginalSizeClicked(bool checked)
114 {
115  height->setEnabled( !checked );
116  width->setEnabled( !checked );
117  keepImageRatio->setEnabled( !checked );
118  //Update default size
119  _k_slotUrlChanged( imageUrlRequester->text() );
120 }
121 
122 void InsertImageWidgetPrivate::_k_slotUrlChanged(const QString& text)
123 {
124  q->enableButtonOk( !text.isEmpty() );
125  QImage image( text );
126  if ( !image.isNull() ) {
127  height->setValue( image.height() );
128  width->setValue( image.width() );
129 
130  imageRatio = (double)( (double)image.height() / (double)image.width() );
131  } else {
132  imageRatio = -1;
133  }
134  q->enableButtonOk(!text.isEmpty() && !image.isNull());
135 }
136 
137 void InsertImageWidgetPrivate::_k_slotImageWidthChanged(int value)
138 {
139  if ( keepImageRatio->isChecked() && !keepOriginalSize->isChecked() ) {
140  if ( imageRatio != -1 ) {
141  height->blockSignals( true );
142  height->setValue( value * imageRatio );
143  height->blockSignals( false );
144  }
145  }
146 }
147 
148 void InsertImageWidgetPrivate::_k_slotImageHeightChanged(int value)
149 {
150  if ( keepImageRatio->isChecked()&& !keepOriginalSize->isChecked() ) {
151  if ( imageRatio != -1 ) {
152  width->blockSignals( true );
153  width->setValue( value / imageRatio );
154  width->blockSignals( false );
155  }
156  }
157 }
158 
159 InsertImageWidget::InsertImageWidget(QWidget *parent)
160  : QWidget(parent), d(new InsertImageWidgetPrivate(this))
161 {
162 }
163 
164 InsertImageWidget::~InsertImageWidget()
165 {
166  delete d;
167 }
168 
169 int InsertImageWidget::imageWidth() const
170 {
171  return d->width->value();
172 }
173 
174 int InsertImageWidget::imageHeight() const
175 {
176  return d->height->value();
177 }
178 
179 void InsertImageWidget::setImageWidth(int value)
180 {
181  d->width->setValue( value );
182 }
183 
184 void InsertImageWidget::setImageHeight(int value)
185 {
186  d->height->setValue( value );
187 }
188 
189 KUrl InsertImageWidget::imageUrl() const
190 {
191  return d->imageUrlRequester->url();
192 }
193 
194 void InsertImageWidget::setImageUrl(const KUrl&url)
195 {
196  d->imageUrlRequester->setUrl( url );
197 }
198 
199 bool InsertImageWidget::keepOriginalSize() const
200 {
201  return d->keepOriginalSize->isChecked();
202 }
203 
204 
205 }
206 
207 #include "insertimagewidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:01 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPIMTextedit Library

Skip menu "KPIMTextedit Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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