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 | |
22 | #include <KFileDialog> |
23 | #include <KImageIO> |
24 | #include <KLocalizedString> |
25 | #include <KUrlRequester> |
26 | #include <KSeparator> |
27 | |
28 | #include <KLineEdit> |
29 | #include <QCheckBox> |
30 | #include <QHBoxLayout> |
31 | #include <QLabel> |
32 | #include <QSpinBox> |
33 | #include <QVBoxLayout> |
34 | |
35 | namespace KPIMTextEdit { |
36 | |
37 | class InsertImageWidgetPrivate |
38 | { |
39 | public: |
40 | InsertImageWidgetPrivate( InsertImageWidget *qq ) |
41 | : imageRatio( -1 ), q( qq ) |
42 | { |
43 | QVBoxLayout *lay = new QVBoxLayout( q ); |
44 | QHBoxLayout *hbox = new QHBoxLayout; |
45 | QLabel *lab = new QLabel( i18n( "Image Location:" ) ); |
46 | imageUrlRequester = new KUrlRequester; |
47 | |
48 | const QStringList mimetypes = KImageIO::mimeTypes( KImageIO::Reading ); |
49 | imageUrlRequester->fileDialog()->setFilter( mimetypes.join( QLatin1String( " " ) ) ); |
50 | imageUrlRequester->fileDialog()->setOperationMode( KFileDialog::Other ); |
51 | imageUrlRequester->fileDialog()->setCaption( i18n( "Add Image" ) ); |
52 | imageUrlRequester->fileDialog()->okButton()->setGuiItem( |
53 | KGuiItem( i18n( "&Add" ), QLatin1String( "document-open" ) ) ); |
54 | imageUrlRequester->fileDialog()->setMode( KFile::File ); |
55 | q->connect( imageUrlRequester->lineEdit(), SIGNAL(textChanged(QString)), |
56 | q, SLOT(_k_slotUrlChanged(QString)) ); |
57 | |
58 | hbox->addWidget( lab ); |
59 | hbox->addWidget( imageUrlRequester ); |
60 | lab->setBuddy( imageUrlRequester ); |
61 | lay->addLayout( hbox ); |
62 | |
63 | keepOriginalSize = new QCheckBox( i18n( "Keep Original Size" ) ); |
64 | q->connect( keepOriginalSize, SIGNAL(clicked(bool)), |
65 | q, SLOT(_k_slotKeepOriginalSizeClicked(bool)) ); |
66 | keepOriginalSize->setChecked( true ); |
67 | lay->addWidget( keepOriginalSize ); |
68 | |
69 | keepImageRatio = new QCheckBox( i18n( "Keep Image Ratio" ) ); |
70 | keepImageRatio->setChecked( true ); |
71 | keepImageRatio->setEnabled( false ); |
72 | lay->addWidget( keepImageRatio ); |
73 | |
74 | hbox = new QHBoxLayout; |
75 | lab = new QLabel( i18n( "Width:" ) ); |
76 | width = new QSpinBox; |
77 | width->setMinimum( 1 ); |
78 | width->setMaximum( 99999 ); |
79 | width->setEnabled( false ); |
80 | width->setSuffix( i18n( " px" ) ); |
81 | lab->setBuddy( width ); |
82 | q->connect( width, SIGNAL(valueChanged(int)), |
83 | q, SLOT(_k_slotImageWidthChanged(int)) ); |
84 | hbox->addWidget( lab ); |
85 | hbox->addWidget( width ); |
86 | lay->addLayout( hbox ); |
87 | |
88 | hbox = new QHBoxLayout; |
89 | lab = new QLabel( i18n( "Height:" ) ); |
90 | height = new QSpinBox; |
91 | height->setMinimum( 1 ); |
92 | height->setMaximum( 99999 ); |
93 | height->setEnabled( false ); |
94 | height->setSuffix( i18n( " px" ) ); |
95 | lab->setBuddy( height ); |
96 | q->connect( height, SIGNAL(valueChanged(int)), |
97 | q, SLOT(_k_slotImageHeightChanged(int)) ); |
98 | hbox->addWidget( lab ); |
99 | hbox->addWidget( height ); |
100 | lay->addLayout( hbox ); |
101 | |
102 | KSeparator *sep = new KSeparator; |
103 | lay->addWidget( sep ); |
104 | |
105 | hbox = new QHBoxLayout; |
106 | lab = new QLabel( i18n("Image Preview:" )); |
107 | hbox->addWidget(lab); |
108 | preview = new QLabel; |
109 | preview->setFrameStyle(QFrame::Box); |
110 | preview->setFixedSize(50,50); |
111 | hbox->addWidget(preview); |
112 | lay->addLayout( hbox ); |
113 | } |
114 | |
115 | void _k_slotKeepOriginalSizeClicked( bool ); |
116 | void _k_slotUrlChanged( const QString & ); |
117 | void _k_slotImageWidthChanged( int ); |
118 | void _k_slotImageHeightChanged( int ); |
119 | |
120 | qreal imageRatio; |
121 | QCheckBox *keepOriginalSize; |
122 | QCheckBox *keepImageRatio; |
123 | QSpinBox *width; |
124 | QSpinBox *height; |
125 | KUrlRequester *imageUrlRequester; |
126 | QLabel *preview; |
127 | InsertImageWidget *q; |
128 | }; |
129 | |
130 | void InsertImageWidgetPrivate::_k_slotKeepOriginalSizeClicked( bool checked ) |
131 | { |
132 | height->setEnabled( !checked ); |
133 | width->setEnabled( !checked ); |
134 | keepImageRatio->setEnabled( !checked ); |
135 | //Update default size |
136 | _k_slotUrlChanged( imageUrlRequester->text() ); |
137 | } |
138 | |
139 | void InsertImageWidgetPrivate::_k_slotUrlChanged( const QString &text ) |
140 | { |
141 | QImage image( text ); |
142 | if ( !image.isNull() ) { |
143 | height->setValue( image.height() ); |
144 | width->setValue( image.width() ); |
145 | |
146 | imageRatio = (double)( (double)image.height() / (double)image.width() ); |
147 | preview->setPixmap(QPixmap::fromImage(image)); |
148 | } else { |
149 | preview->clear(); |
150 | imageRatio = -1; |
151 | } |
152 | q->enableButtonOk( !text.isEmpty() ); |
153 | } |
154 | |
155 | void InsertImageWidgetPrivate::_k_slotImageWidthChanged( int value ) |
156 | { |
157 | if ( keepImageRatio->isChecked() && !keepOriginalSize->isChecked() ) { |
158 | if ( imageRatio != -1 ) { |
159 | height->blockSignals( true ); |
160 | height->setValue( value * imageRatio ); |
161 | height->blockSignals( false ); |
162 | } |
163 | } |
164 | } |
165 | |
166 | void InsertImageWidgetPrivate::_k_slotImageHeightChanged( int value ) |
167 | { |
168 | if ( keepImageRatio->isChecked() && !keepOriginalSize->isChecked() ) { |
169 | if ( imageRatio != -1 ) { |
170 | width->blockSignals( true ); |
171 | width->setValue( value / imageRatio ); |
172 | width->blockSignals( false ); |
173 | } |
174 | } |
175 | } |
176 | |
177 | InsertImageWidget::InsertImageWidget( QWidget *parent ) |
178 | : QWidget( parent ), d( new InsertImageWidgetPrivate( this ) ) |
179 | { |
180 | } |
181 | |
182 | InsertImageWidget::~InsertImageWidget() |
183 | { |
184 | delete d; |
185 | } |
186 | |
187 | int InsertImageWidget::imageWidth() const |
188 | { |
189 | return d->width->value(); |
190 | } |
191 | |
192 | int InsertImageWidget::imageHeight() const |
193 | { |
194 | return d->height->value(); |
195 | } |
196 | |
197 | void InsertImageWidget::setImageWidth( int value ) |
198 | { |
199 | d->width->setValue( value ); |
200 | } |
201 | |
202 | void InsertImageWidget::setImageHeight( int value ) |
203 | { |
204 | d->height->setValue( value ); |
205 | } |
206 | |
207 | KUrl InsertImageWidget::imageUrl() const |
208 | { |
209 | return d->imageUrlRequester->url(); |
210 | } |
211 | |
212 | void InsertImageWidget::setImageUrl( const KUrl &url ) |
213 | { |
214 | d->imageUrlRequester->setUrl( url ); |
215 | } |
216 | |
217 | bool InsertImageWidget::keepOriginalSize() const |
218 | { |
219 | return d->keepOriginalSize->isChecked(); |
220 | } |
221 | |
222 | } |
223 | |
224 | #include "moc_insertimagewidget.cpp" |
225 | |