-rw-r--r-- | noncore/multimedia/showimg/showimg.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/multimedia/showimg/showimg.cpp b/noncore/multimedia/showimg/showimg.cpp index c56994d..7c43e11 100644 --- a/noncore/multimedia/showimg/showimg.cpp +++ b/noncore/multimedia/showimg/showimg.cpp | |||
@@ -1,557 +1,557 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | // | 21 | // |
22 | // Full-screen and rotation options contributed by Robert Wittams <robert@wittams.com> | 22 | // Full-screen and rotation options contributed by Robert Wittams <robert@wittams.com> |
23 | // | 23 | // |
24 | 24 | ||
25 | #include "showimg.h" | 25 | #include "showimg.h" |
26 | 26 | ||
27 | #include <qpe/resource.h> | 27 | #include <qpe/resource.h> |
28 | #include <qpe/fileselector.h> | 28 | #include <qpe/fileselector.h> |
29 | #include <qpe/applnk.h> | 29 | #include <qpe/applnk.h> |
30 | 30 | ||
31 | #include <qpe/qpemenubar.h> | 31 | #include <qpe/qpemenubar.h> |
32 | #include <qwidgetstack.h> | 32 | #include <qwidgetstack.h> |
33 | #include <qpe/qpetoolbar.h> | 33 | #include <qpe/qpetoolbar.h> |
34 | #include <qaction.h> | 34 | #include <qaction.h> |
35 | #include <qfiledialog.h> | 35 | #include <qfiledialog.h> |
36 | #include <qmessagebox.h> | 36 | #include <qmessagebox.h> |
37 | #include <qpopupmenu.h> | 37 | #include <qpopupmenu.h> |
38 | #include <qlabel.h> | 38 | #include <qlabel.h> |
39 | #include <qpainter.h> | 39 | #include <qpainter.h> |
40 | #include <qkeycode.h> | 40 | #include <qkeycode.h> |
41 | #include <qapplication.h> | 41 | #include <qapplication.h> |
42 | #include <qclipboard.h> | 42 | #include <qclipboard.h> |
43 | #include <qtimer.h> | 43 | #include <qtimer.h> |
44 | 44 | ||
45 | 45 | ||
46 | ImagePane::ImagePane( QWidget *parent=0 ) : QWidget( parent ) | 46 | ImagePane::ImagePane( QWidget *parent ) : QWidget( parent ) |
47 | { | 47 | { |
48 | vb = new QVBoxLayout( this ); | 48 | vb = new QVBoxLayout( this ); |
49 | 49 | ||
50 | image = new ImageWidget( this ); | 50 | image = new ImageWidget( this ); |
51 | connect(image, SIGNAL( clicked() ), this, SLOT( imageClicked() )); | 51 | connect(image, SIGNAL( clicked() ), this, SLOT( imageClicked() )); |
52 | 52 | ||
53 | vb->addWidget( image ); | 53 | vb->addWidget( image ); |
54 | 54 | ||
55 | status = new QLabel( this ); | 55 | status = new QLabel( this ); |
56 | status->setFixedHeight( fontMetrics().height() + 4 ); | 56 | status->setFixedHeight( fontMetrics().height() + 4 ); |
57 | vb->addWidget( status ); | 57 | vb->addWidget( status ); |
58 | } | 58 | } |
59 | 59 | ||
60 | void ImagePane::setPixmap( const QPixmap &pm ) | 60 | void ImagePane::setPixmap( const QPixmap &pm ) |
61 | { | 61 | { |
62 | image->setPixmap( pm ); | 62 | image->setPixmap( pm ); |
63 | image->repaint( false ); | 63 | image->repaint( false ); |
64 | } | 64 | } |
65 | 65 | ||
66 | void ImagePane::imageClicked() | 66 | void ImagePane::imageClicked() |
67 | { | 67 | { |
68 | emit clicked(); | 68 | emit clicked(); |
69 | } | 69 | } |
70 | 70 | ||
71 | void ImagePane::showStatus() | 71 | void ImagePane::showStatus() |
72 | { | 72 | { |
73 | delete vb; | 73 | delete vb; |
74 | vb = new QVBoxLayout( this ); | 74 | vb = new QVBoxLayout( this ); |
75 | vb->addWidget( image ); | 75 | vb->addWidget( image ); |
76 | status->show(); | 76 | status->show(); |
77 | vb->addWidget( status ); | 77 | vb->addWidget( status ); |
78 | } | 78 | } |
79 | 79 | ||
80 | 80 | ||
81 | void ImagePane::hideStatus() | 81 | void ImagePane::hideStatus() |
82 | { | 82 | { |
83 | delete vb; | 83 | delete vb; |
84 | vb = new QVBoxLayout( this ); | 84 | vb = new QVBoxLayout( this ); |
85 | vb->addWidget( image ); | 85 | vb->addWidget( image ); |
86 | status->hide(); | 86 | status->hide(); |
87 | } | 87 | } |
88 | 88 | ||
89 | //=========================================================================== | 89 | //=========================================================================== |
90 | /* | 90 | /* |
91 | Draws the portion of the scaled pixmap that needs to be updated | 91 | Draws the portion of the scaled pixmap that needs to be updated |
92 | */ | 92 | */ |
93 | 93 | ||
94 | void ImageWidget::paintEvent( QPaintEvent *e ) | 94 | void ImageWidget::paintEvent( QPaintEvent *e ) |
95 | { | 95 | { |
96 | QPainter painter(this); | 96 | QPainter painter(this); |
97 | 97 | ||
98 | painter.setClipRect(e->rect()); | 98 | painter.setClipRect(e->rect()); |
99 | painter.setBrush( black ); | 99 | painter.setBrush( black ); |
100 | painter.drawRect( 0, 0, width(), height() ); | 100 | painter.drawRect( 0, 0, width(), height() ); |
101 | 101 | ||
102 | if ( pixmap.size() != QSize( 0, 0 ) ) { // is an image loaded? | 102 | if ( pixmap.size() != QSize( 0, 0 ) ) { // is an image loaded? |
103 | painter.drawPixmap((width() - pixmap.width()) / 2, (height() - pixmap.height()) / 2, pixmap); | 103 | painter.drawPixmap((width() - pixmap.width()) / 2, (height() - pixmap.height()) / 2, pixmap); |
104 | } | 104 | } |
105 | } | 105 | } |
106 | 106 | ||
107 | void ImageWidget::mouseReleaseEvent(QMouseEvent *) | 107 | void ImageWidget::mouseReleaseEvent(QMouseEvent *) |
108 | { | 108 | { |
109 | emit clicked(); | 109 | emit clicked(); |
110 | } | 110 | } |
111 | 111 | ||
112 | 112 | ||
113 | //=========================================================================== | 113 | //=========================================================================== |
114 | 114 | ||
115 | ImageViewer::ImageViewer( QWidget *parent, const char *name, int wFlags ) | 115 | ImageViewer::ImageViewer( QWidget *parent, const char *name, int wFlags ) |
116 | : QMainWindow( parent, name, wFlags ), filename( 0 ), | 116 | : QMainWindow( parent, name, wFlags ), filename( 0 ), |
117 | pickx( -1 ), picky( -1 ), clickx( -1 ), clicky( -1 ), bFromDocView( FALSE ) | 117 | pickx( -1 ), picky( -1 ), clickx( -1 ), clicky( -1 ), bFromDocView( FALSE ) |
118 | { | 118 | { |
119 | setCaption( tr("Image Viewer") ); | 119 | setCaption( tr("Image Viewer") ); |
120 | setIcon( Resource::loadPixmap( "ImageViewer" ) ); | 120 | setIcon( Resource::loadPixmap( "ImageViewer" ) ); |
121 | 121 | ||
122 | isFullScreen = FALSE; | 122 | isFullScreen = FALSE; |
123 | 123 | ||
124 | setToolBarsMovable( FALSE ); | 124 | setToolBarsMovable( FALSE ); |
125 | 125 | ||
126 | toolBar = new QPEToolBar( this ); | 126 | toolBar = new QPEToolBar( this ); |
127 | toolBar->setHorizontalStretchable( TRUE ); | 127 | toolBar->setHorizontalStretchable( TRUE ); |
128 | 128 | ||
129 | menubar = new QPEMenuBar( toolBar ); | 129 | menubar = new QPEMenuBar( toolBar ); |
130 | 130 | ||
131 | QStrList fmt = QImage::outputFormats(); | 131 | QStrList fmt = QImage::outputFormats(); |
132 | 132 | ||
133 | QPopupMenu *edit = new QPopupMenu( menubar ); | 133 | QPopupMenu *edit = new QPopupMenu( menubar ); |
134 | QPopupMenu *view = new QPopupMenu( menubar ); | 134 | QPopupMenu *view = new QPopupMenu( menubar ); |
135 | 135 | ||
136 | menubar->insertItem( "Edit", edit ); | 136 | menubar->insertItem( "Edit", edit ); |
137 | menubar->insertItem( "View", view ); | 137 | menubar->insertItem( "View", view ); |
138 | 138 | ||
139 | edit->insertItem(tr("Horizontal flip"), this, SLOT(hFlip()), 0); | 139 | edit->insertItem(tr("Horizontal flip"), this, SLOT(hFlip()), 0); |
140 | edit->insertItem(tr("Vertical flip"), this, SLOT(vFlip()), 0); | 140 | edit->insertItem(tr("Vertical flip"), this, SLOT(vFlip()), 0); |
141 | 141 | ||
142 | stack = new QWidgetStack( this ); | 142 | stack = new QWidgetStack( this ); |
143 | stack->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); | 143 | stack->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); |
144 | setCentralWidget( stack ); | 144 | setCentralWidget( stack ); |
145 | 145 | ||
146 | imagePanel = new ImagePane( stack ); | 146 | imagePanel = new ImagePane( stack ); |
147 | connect(imagePanel, SIGNAL(clicked()), this, SLOT(normalView())); | 147 | connect(imagePanel, SIGNAL(clicked()), this, SLOT(normalView())); |
148 | 148 | ||
149 | fileSelector = new FileSelector("image/*", stack, "fs"); | 149 | fileSelector = new FileSelector("image/*", stack, "fs"); |
150 | fileSelector->setNewVisible(FALSE); | 150 | fileSelector->setNewVisible(FALSE); |
151 | fileSelector->setCloseVisible(FALSE); | 151 | fileSelector->setCloseVisible(FALSE); |
152 | connect( fileSelector, SIGNAL( closeMe() ), this, SLOT( closeFileSelector() ) ); | 152 | connect( fileSelector, SIGNAL( closeMe() ), this, SLOT( closeFileSelector() ) ); |
153 | connect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( openFile( const DocLnk & ) ) ); | 153 | connect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( openFile( const DocLnk & ) ) ); |
154 | 154 | ||
155 | toolBar = new QPEToolBar( this ); | 155 | toolBar = new QPEToolBar( this ); |
156 | 156 | ||
157 | QAction *a; | 157 | QAction *a; |
158 | 158 | ||
159 | a = new QAction( tr( "Open" ), Resource::loadPixmap( "fileopen" ), QString::null, 0, this, 0 ); | 159 | a = new QAction( tr( "Open" ), Resource::loadPixmap( "fileopen" ), QString::null, 0, this, 0 ); |
160 | connect( a, SIGNAL( activated() ), this, SLOT( open() ) ); | 160 | connect( a, SIGNAL( activated() ), this, SLOT( open() ) ); |
161 | a->addTo( toolBar ); | 161 | a->addTo( toolBar ); |
162 | 162 | ||
163 | a = new QAction( tr( "Rotate 180" ), Resource::loadPixmap( "repeat" ), QString::null, 0, this, 0 ); | 163 | a = new QAction( tr( "Rotate 180" ), Resource::loadPixmap( "repeat" ), QString::null, 0, this, 0 ); |
164 | connect( a, SIGNAL( activated() ), this, SLOT( rot180() ) ); | 164 | connect( a, SIGNAL( activated() ), this, SLOT( rot180() ) ); |
165 | a->addTo( toolBar ); | 165 | a->addTo( toolBar ); |
166 | a->addTo( edit ); | 166 | a->addTo( edit ); |
167 | 167 | ||
168 | a = new QAction( tr( "Rotate 90"), Resource::loadPixmap( "rotate90" ), QString::null, 0, this, 0); | 168 | a = new QAction( tr( "Rotate 90"), Resource::loadPixmap( "rotate90" ), QString::null, 0, this, 0); |
169 | connect( a, SIGNAL( activated() ), this, SLOT( rot90() ) ); | 169 | connect( a, SIGNAL( activated() ), this, SLOT( rot90() ) ); |
170 | a->addTo( toolBar ); | 170 | a->addTo( toolBar ); |
171 | a->addTo( edit ); | 171 | a->addTo( edit ); |
172 | 172 | ||
173 | a = new QAction( tr( "Fullscreen" ), Resource::loadPixmap( "fullscreen" ), QString::null, 0, this, 0 ); | 173 | a = new QAction( tr( "Fullscreen" ), Resource::loadPixmap( "fullscreen" ), QString::null, 0, this, 0 ); |
174 | connect( a, SIGNAL( activated() ), this, SLOT( fullScreen() ) ); | 174 | connect( a, SIGNAL( activated() ), this, SLOT( fullScreen() ) ); |
175 | a->addTo( toolBar ); | 175 | a->addTo( toolBar ); |
176 | a->addTo( view); | 176 | a->addTo( view); |
177 | 177 | ||
178 | stack->raiseWidget( fileSelector ); | 178 | stack->raiseWidget( fileSelector ); |
179 | 179 | ||
180 | setMouseTracking( TRUE ); | 180 | setMouseTracking( TRUE ); |
181 | } | 181 | } |
182 | 182 | ||
183 | ImageViewer::~ImageViewer() | 183 | ImageViewer::~ImageViewer() |
184 | { | 184 | { |
185 | delete imagePanel; // in case it is fullscreen | 185 | delete imagePanel; // in case it is fullscreen |
186 | } | 186 | } |
187 | 187 | ||
188 | void ImageViewer::setDocument(const QString& fileref) | 188 | void ImageViewer::setDocument(const QString& fileref) |
189 | { | 189 | { |
190 | delayLoad = fileref; | 190 | delayLoad = fileref; |
191 | stack->raiseWidget(imagePanel); | 191 | stack->raiseWidget(imagePanel); |
192 | QTimer::singleShot( 0, this, SLOT(doDelayedLoad()) ); | 192 | QTimer::singleShot( 0, this, SLOT(doDelayedLoad()) ); |
193 | } | 193 | } |
194 | 194 | ||
195 | void ImageViewer::doDelayedLoad() | 195 | void ImageViewer::doDelayedLoad() |
196 | { | 196 | { |
197 | show(delayLoad); | 197 | show(delayLoad); |
198 | } | 198 | } |
199 | 199 | ||
200 | void ImageViewer::show() | 200 | void ImageViewer::show() |
201 | { | 201 | { |
202 | normalView(); | 202 | normalView(); |
203 | QMainWindow::show(); | 203 | QMainWindow::show(); |
204 | } | 204 | } |
205 | 205 | ||
206 | void ImageViewer::show(const QString& fileref) | 206 | void ImageViewer::show(const QString& fileref) |
207 | { | 207 | { |
208 | bFromDocView = TRUE; | 208 | bFromDocView = TRUE; |
209 | closeFileSelector(); | 209 | closeFileSelector(); |
210 | DocLnk link(fileref); | 210 | DocLnk link(fileref); |
211 | if ( link.isValid() ) { | 211 | if ( link.isValid() ) { |
212 | openFile(link); | 212 | openFile(link); |
213 | } else { | 213 | } else { |
214 | filename = fileref; | 214 | filename = fileref; |
215 | updateCaption( fileref ); | 215 | updateCaption( fileref ); |
216 | loadImage( fileref ); | 216 | loadImage( fileref ); |
217 | } | 217 | } |
218 | } | 218 | } |
219 | 219 | ||
220 | void ImageViewer::openFile( const DocLnk &file ) | 220 | void ImageViewer::openFile( const DocLnk &file ) |
221 | { | 221 | { |
222 | closeFileSelector(); | 222 | closeFileSelector(); |
223 | DocLnk link(file); | 223 | DocLnk link(file); |
224 | updateCaption( link.name() ); | 224 | updateCaption( link.name() ); |
225 | loadImage( link.file() ); | 225 | loadImage( link.file() ); |
226 | } | 226 | } |
227 | 227 | ||
228 | void ImageViewer::open() | 228 | void ImageViewer::open() |
229 | { | 229 | { |
230 | stack->raiseWidget(fileSelector); | 230 | stack->raiseWidget(fileSelector); |
231 | } | 231 | } |
232 | 232 | ||
233 | void ImageViewer::closeFileSelector() | 233 | void ImageViewer::closeFileSelector() |
234 | { | 234 | { |
235 | stack->raiseWidget(imagePanel); | 235 | stack->raiseWidget(imagePanel); |
236 | } | 236 | } |
237 | 237 | ||
238 | void ImageViewer::updateCaption( QString name ) | 238 | void ImageViewer::updateCaption( QString name ) |
239 | { | 239 | { |
240 | int sep = name.findRev( '/' ); | 240 | int sep = name.findRev( '/' ); |
241 | if ( sep >= 0 ) | 241 | if ( sep >= 0 ) |
242 | name = name.mid( sep+1 ); | 242 | name = name.mid( sep+1 ); |
243 | setCaption( name + tr(" - Image Viewer") ); | 243 | setCaption( name + tr(" - Image Viewer") ); |
244 | } | 244 | } |
245 | 245 | ||
246 | /* | 246 | /* |
247 | This function loads an image from a file. | 247 | This function loads an image from a file. |
248 | */ | 248 | */ |
249 | 249 | ||
250 | void ImageViewer::loadImage( const char *fileName ) | 250 | void ImageViewer::loadImage( const char *fileName ) |
251 | { | 251 | { |
252 | filename = fileName; | 252 | filename = fileName; |
253 | if ( filename ) { | 253 | if ( filename ) { |
254 | QApplication::setOverrideCursor( waitCursor ); // this might take time | 254 | QApplication::setOverrideCursor( waitCursor ); // this might take time |
255 | imagePanel->statusLabel()->setText( tr("Loading image...") ); | 255 | imagePanel->statusLabel()->setText( tr("Loading image...") ); |
256 | qApp->processEvents(); | 256 | qApp->processEvents(); |
257 | bool ok = image.load(filename, 0); | 257 | bool ok = image.load(filename, 0); |
258 | pickx = -1; | 258 | pickx = -1; |
259 | clickx = -1; | 259 | clickx = -1; |
260 | if ( ok ) | 260 | if ( ok ) |
261 | ok = reconvertImage(); | 261 | ok = reconvertImage(); |
262 | if ( !ok ) { | 262 | if ( !ok ) { |
263 | pm.resize(0,0); // couldn't load image | 263 | pm.resize(0,0); // couldn't load image |
264 | update(); | 264 | update(); |
265 | } | 265 | } |
266 | QApplication::restoreOverrideCursor();// restore original cursor | 266 | QApplication::restoreOverrideCursor();// restore original cursor |
267 | } | 267 | } |
268 | updateStatus(); | 268 | updateStatus(); |
269 | imagePanel->setPixmap( pmScaled ); | 269 | imagePanel->setPixmap( pmScaled ); |
270 | stack->raiseWidget(imagePanel); | 270 | stack->raiseWidget(imagePanel); |
271 | } | 271 | } |
272 | 272 | ||
273 | bool ImageViewer::loadSelected() | 273 | bool ImageViewer::loadSelected() |
274 | { | 274 | { |
275 | bool ok = false; | 275 | bool ok = false; |
276 | if ( stack->visibleWidget() == fileSelector ) { | 276 | if ( stack->visibleWidget() == fileSelector ) { |
277 | const DocLnk *link = fileSelector->selected(); | 277 | const DocLnk *link = fileSelector->selected(); |
278 | if ( link ) { | 278 | if ( link ) { |
279 | if ( link->file() != filename ) { | 279 | if ( link->file() != filename ) { |
280 | updateCaption( link->name() ); | 280 | updateCaption( link->name() ); |
281 | filename = link->file(); | 281 | filename = link->file(); |
282 | imagePanel->statusLabel()->setText( tr("Loading image...") ); | 282 | imagePanel->statusLabel()->setText( tr("Loading image...") ); |
283 | qApp->processEvents(); | 283 | qApp->processEvents(); |
284 | ok = image.load(filename, 0); | 284 | ok = image.load(filename, 0); |
285 | if ( ok ) | 285 | if ( ok ) |
286 | ok = reconvertImage(); | 286 | ok = reconvertImage(); |
287 | if ( !ok ) | 287 | if ( !ok ) |
288 | pm.resize(0,0); | 288 | pm.resize(0,0); |
289 | } | 289 | } |
290 | } | 290 | } |
291 | } | 291 | } |
292 | if ( !image.isNull() ) { | 292 | if ( !image.isNull() ) { |
293 | ok = true; | 293 | ok = true; |
294 | closeFileSelector(); | 294 | closeFileSelector(); |
295 | } | 295 | } |
296 | 296 | ||
297 | return ok; | 297 | return ok; |
298 | } | 298 | } |
299 | 299 | ||
300 | bool ImageViewer::reconvertImage() | 300 | bool ImageViewer::reconvertImage() |
301 | { | 301 | { |
302 | bool success = FALSE; | 302 | bool success = FALSE; |
303 | 303 | ||
304 | if ( image.isNull() ) return FALSE; | 304 | if ( image.isNull() ) return FALSE; |
305 | 305 | ||
306 | QApplication::setOverrideCursor( waitCursor ); // this might take time | 306 | QApplication::setOverrideCursor( waitCursor ); // this might take time |
307 | if ( pm.convertFromImage(image /*, conversion_flags */ ) ) | 307 | if ( pm.convertFromImage(image /*, conversion_flags */ ) ) |
308 | { | 308 | { |
309 | pmScaled = QPixmap(); | 309 | pmScaled = QPixmap(); |
310 | scale(); | 310 | scale(); |
311 | success = TRUE; // load successful | 311 | success = TRUE; // load successful |
312 | } else { | 312 | } else { |
313 | pm.resize(0,0); // couldn't load image | 313 | pm.resize(0,0); // couldn't load image |
314 | } | 314 | } |
315 | QApplication::restoreOverrideCursor();// restore original cursor | 315 | QApplication::restoreOverrideCursor();// restore original cursor |
316 | 316 | ||
317 | return success; // TRUE if loaded OK | 317 | return success; // TRUE if loaded OK |
318 | } | 318 | } |
319 | 319 | ||
320 | 320 | ||
321 | int ImageViewer::calcHeight() | 321 | int ImageViewer::calcHeight() |
322 | { | 322 | { |
323 | if ( !isFullScreen) | 323 | if ( !isFullScreen) |
324 | return height() - menubar->heightForWidth( width() ) | 324 | return height() - menubar->heightForWidth( width() ) |
325 | - imagePanel->statusLabel()->height(); | 325 | - imagePanel->statusLabel()->height(); |
326 | else | 326 | else |
327 | return qApp->desktop()->height(); | 327 | return qApp->desktop()->height(); |
328 | } | 328 | } |
329 | /* | 329 | /* |
330 | This functions scales the pixmap in the member variable "pm" to fit the | 330 | This functions scales the pixmap in the member variable "pm" to fit the |
331 | widget size and puts the resulting pixmap in the member variable "pmScaled". | 331 | widget size and puts the resulting pixmap in the member variable "pmScaled". |
332 | */ | 332 | */ |
333 | 333 | ||
334 | void ImageViewer::scale() | 334 | void ImageViewer::scale() |
335 | { | 335 | { |
336 | int h = calcHeight(); | 336 | int h = calcHeight(); |
337 | if ( image.isNull() ) return; | 337 | if ( image.isNull() ) return; |
338 | 338 | ||
339 | QApplication::setOverrideCursor( waitCursor ); // this might take time | 339 | QApplication::setOverrideCursor( waitCursor ); // this might take time |
340 | if ( width() == pm.width() && h == pm.height() ) { // no need to scale if widget | 340 | if ( width() == pm.width() && h == pm.height() ) { // no need to scale if widget |
341 | pmScaled = pm; // size equals pixmap size | 341 | pmScaled = pm; // size equals pixmap size |
342 | } else { | 342 | } else { |
343 | double hs = (double)h / (double)image.height(); | 343 | double hs = (double)h / (double)image.height(); |
344 | double ws = (double)width() / (double)image.width(); | 344 | double ws = (double)width() / (double)image.width(); |
345 | double scaleFactor = (hs > ws) ? ws : hs; | 345 | double scaleFactor = (hs > ws) ? ws : hs; |
346 | int smoothW = (int)(scaleFactor * image.width()); | 346 | int smoothW = (int)(scaleFactor * image.width()); |
347 | int smoothH = (int)(scaleFactor * image.height()); | 347 | int smoothH = (int)(scaleFactor * image.height()); |
348 | 348 | ||
349 | pmScaled.convertFromImage( image.smoothScale( smoothW, smoothH ) /*, conversion_flags */ ); | 349 | pmScaled.convertFromImage( image.smoothScale( smoothW, smoothH ) /*, conversion_flags */ ); |
350 | } | 350 | } |
351 | QApplication::restoreOverrideCursor();// restore original cursor | 351 | QApplication::restoreOverrideCursor();// restore original cursor |
352 | } | 352 | } |
353 | 353 | ||
354 | /* | 354 | /* |
355 | The resize event handler, if a valid pixmap was loaded it will call | 355 | The resize event handler, if a valid pixmap was loaded it will call |
356 | scale() to fit the pixmap to the new widget size. | 356 | scale() to fit the pixmap to the new widget size. |
357 | */ | 357 | */ |
358 | 358 | ||
359 | void ImageViewer::resizeEvent( QResizeEvent * ) | 359 | void ImageViewer::resizeEvent( QResizeEvent * ) |
360 | { | 360 | { |
361 | imagePanel->statusLabel()->setGeometry(0, height() - imagePanel->statusLabel()->height(), | 361 | imagePanel->statusLabel()->setGeometry(0, height() - imagePanel->statusLabel()->height(), |
362 | width(), imagePanel->statusLabel()->height()); | 362 | width(), imagePanel->statusLabel()->height()); |
363 | 363 | ||
364 | if ( pm.size() == QSize( 0, 0 ) ) // we couldn't load the image | 364 | if ( pm.size() == QSize( 0, 0 ) ) // we couldn't load the image |
365 | return; | 365 | return; |
366 | 366 | ||
367 | int h = calcHeight(); | 367 | int h = calcHeight(); |
368 | 368 | ||
369 | if ( width() != pmScaled.width() || h != pmScaled.height()) | 369 | if ( width() != pmScaled.width() || h != pmScaled.height()) |
370 | { // if new size, | 370 | { // if new size, |
371 | scale(); // scale pmScaled to window | 371 | scale(); // scale pmScaled to window |
372 | updateStatus(); | 372 | updateStatus(); |
373 | } | 373 | } |
374 | if ( image.hasAlphaBuffer() ) | 374 | if ( image.hasAlphaBuffer() ) |
375 | erase(); | 375 | erase(); |
376 | } | 376 | } |
377 | 377 | ||
378 | void ImageViewer::convertEvent( QMouseEvent* e, int& x, int& y) | 378 | void ImageViewer::convertEvent( QMouseEvent* e, int& x, int& y) |
379 | { | 379 | { |
380 | if ( pm.size() != QSize( 0, 0 ) ) { | 380 | if ( pm.size() != QSize( 0, 0 ) ) { |
381 | int h = height() - menubar->heightForWidth( width() ) - imagePanel->statusLabel()->height(); | 381 | int h = height() - menubar->heightForWidth( width() ) - imagePanel->statusLabel()->height(); |
382 | int nx = e->x() * image.width() / width(); | 382 | int nx = e->x() * image.width() / width(); |
383 | int ny = (e->y()-menubar->heightForWidth( width() )) * image.height() / h; | 383 | int ny = (e->y()-menubar->heightForWidth( width() )) * image.height() / h; |
384 | if (nx != x || ny != y ) { | 384 | if (nx != x || ny != y ) { |
385 | x = nx; | 385 | x = nx; |
386 | y = ny; | 386 | y = ny; |
387 | updateStatus(); | 387 | updateStatus(); |
388 | } | 388 | } |
389 | } | 389 | } |
390 | } | 390 | } |
391 | 391 | ||
392 | void ImageViewer::mousePressEvent( QMouseEvent *e ) | 392 | void ImageViewer::mousePressEvent( QMouseEvent *e ) |
393 | { | 393 | { |
394 | convertEvent(e, clickx, clicky); | 394 | convertEvent(e, clickx, clicky); |
395 | } | 395 | } |
396 | 396 | ||
397 | void ImageViewer::mouseMoveEvent( QMouseEvent *e ) | 397 | void ImageViewer::mouseMoveEvent( QMouseEvent *e ) |
398 | { | 398 | { |
399 | convertEvent( e, pickx, picky ); | 399 | convertEvent( e, pickx, picky ); |
400 | } | 400 | } |
401 | 401 | ||
402 | void ImageViewer::hFlip() | 402 | void ImageViewer::hFlip() |
403 | { | 403 | { |
404 | if ( loadSelected() ) | 404 | if ( loadSelected() ) |
405 | setImage(image.mirror(TRUE,FALSE)); | 405 | setImage(image.mirror(TRUE,FALSE)); |
406 | } | 406 | } |
407 | 407 | ||
408 | void ImageViewer::vFlip() | 408 | void ImageViewer::vFlip() |
409 | { | 409 | { |
410 | if ( loadSelected() ) | 410 | if ( loadSelected() ) |
411 | setImage(image.mirror(FALSE,TRUE)); | 411 | setImage(image.mirror(FALSE,TRUE)); |
412 | } | 412 | } |
413 | 413 | ||
414 | void ImageViewer::rot180() | 414 | void ImageViewer::rot180() |
415 | { | 415 | { |
416 | if ( loadSelected() ) | 416 | if ( loadSelected() ) |
417 | setImage(image.mirror(TRUE,TRUE)); | 417 | setImage(image.mirror(TRUE,TRUE)); |
418 | } | 418 | } |
419 | 419 | ||
420 | void ImageViewer::rot90() | 420 | void ImageViewer::rot90() |
421 | { | 421 | { |
422 | if ( loadSelected() ) { | 422 | if ( loadSelected() ) { |
423 | QImage oldimage, newimage; | 423 | QImage oldimage, newimage; |
424 | uchar *oldbits, *newbits; | 424 | uchar *oldbits, *newbits; |
425 | int i, j, p; | 425 | int i, j, p; |
426 | int w, h; | 426 | int w, h; |
427 | 427 | ||
428 | oldimage = image.convertDepth(32); | 428 | oldimage = image.convertDepth(32); |
429 | w = oldimage.height(); | 429 | w = oldimage.height(); |
430 | h = oldimage.width(); | 430 | h = oldimage.width(); |
431 | newimage = QImage( w, h, 32); | 431 | newimage = QImage( w, h, 32); |
432 | 432 | ||
433 | oldbits = oldimage.bits(); | 433 | oldbits = oldimage.bits(); |
434 | newbits = newimage.bits(); | 434 | newbits = newimage.bits(); |
435 | 435 | ||
436 | for (i=0; i < w ; i++) | 436 | for (i=0; i < w ; i++) |
437 | for (j=0; j < h; j++) | 437 | for (j=0; j < h; j++) |
438 | for (p = 0 ; p < 4 ; p++) | 438 | for (p = 0 ; p < 4 ; p++) |
439 | newbits[(j * w + i) * 4 + p] = oldbits[ ((i + 1) * h - j ) * 4 + p]; | 439 | newbits[(j * w + i) * 4 + p] = oldbits[ ((i + 1) * h - j ) * 4 + p]; |
440 | 440 | ||
441 | setImage(newimage); | 441 | setImage(newimage); |
442 | } | 442 | } |
443 | } | 443 | } |
444 | 444 | ||
445 | 445 | ||
446 | 446 | ||
447 | void ImageViewer::normalView() | 447 | void ImageViewer::normalView() |
448 | { | 448 | { |
449 | if ( !imagePanel->parentWidget() ) { | 449 | if ( !imagePanel->parentWidget() ) { |
450 | isFullScreen = FALSE; | 450 | isFullScreen = FALSE; |
451 | stack->addWidget( imagePanel, 1 ); | 451 | stack->addWidget( imagePanel, 1 ); |
452 | //imagePanel->reparent(stack,0,QPoint(0,0),FALSE); | 452 | //imagePanel->reparent(stack,0,QPoint(0,0),FALSE); |
453 | //imagePanel->resize(width(), calcHeight()); | 453 | //imagePanel->resize(width(), calcHeight()); |
454 | scale(); | 454 | scale(); |
455 | updateStatus(); | 455 | updateStatus(); |
456 | imagePanel->setPixmap( pmScaled ); | 456 | imagePanel->setPixmap( pmScaled ); |
457 | imagePanel->showStatus(); | 457 | imagePanel->showStatus(); |
458 | //imagePanel->show(); | 458 | //imagePanel->show(); |
459 | stack->raiseWidget( imagePanel ); | 459 | stack->raiseWidget( imagePanel ); |
460 | } | 460 | } |
461 | } | 461 | } |
462 | 462 | ||
463 | void ImageViewer::fullScreen() | 463 | void ImageViewer::fullScreen() |
464 | { | 464 | { |
465 | // Full-screen and rotation options | 465 | // Full-screen and rotation options |
466 | // contributed by Robert Wittams <robert@wittams.com> | 466 | // contributed by Robert Wittams <robert@wittams.com> |
467 | 467 | ||
468 | if ( imagePanel->parentWidget() && loadSelected() ) { | 468 | if ( imagePanel->parentWidget() && loadSelected() ) { |
469 | isFullScreen = TRUE; | 469 | isFullScreen = TRUE; |
470 | imagePanel->reparent(0,QPoint(0,0)); | 470 | imagePanel->reparent(0,QPoint(0,0)); |
471 | imagePanel->resize(qApp->desktop()->width(), qApp->desktop()->height()); | 471 | imagePanel->resize(qApp->desktop()->width(), qApp->desktop()->height()); |
472 | 472 | ||
473 | scale(); | 473 | scale(); |
474 | updateStatus(); | 474 | updateStatus(); |
475 | imagePanel->hideStatus(); | 475 | imagePanel->hideStatus(); |
476 | imagePanel->setPixmap( pmScaled ); | 476 | imagePanel->setPixmap( pmScaled ); |
477 | imagePanel->showFullScreen(); | 477 | imagePanel->showFullScreen(); |
478 | } | 478 | } |
479 | } | 479 | } |
480 | 480 | ||
481 | void ImageViewer::setImage(const QImage& newimage) | 481 | void ImageViewer::setImage(const QImage& newimage) |
482 | { | 482 | { |
483 | image = newimage; | 483 | image = newimage; |
484 | pickx = -1; | 484 | pickx = -1; |
485 | clickx = -1; | 485 | clickx = -1; |
486 | reconvertImage(); | 486 | reconvertImage(); |
487 | imagePanel->setPixmap( pmScaled ); | 487 | imagePanel->setPixmap( pmScaled ); |
488 | updateStatus(); | 488 | updateStatus(); |
489 | } | 489 | } |
490 | 490 | ||
491 | void ImageViewer::updateStatus() | 491 | void ImageViewer::updateStatus() |
492 | { | 492 | { |
493 | if ( pm.size() == QSize( 0, 0 ) ) { | 493 | if ( pm.size() == QSize( 0, 0 ) ) { |
494 | if ( filename ) | 494 | if ( filename ) |
495 | imagePanel->statusLabel()->setText( tr("Could not load image") ); | 495 | imagePanel->statusLabel()->setText( tr("Could not load image") ); |
496 | else | 496 | else |
497 | imagePanel->statusLabel()->setText( tr("No image - select Open from File menu.") ); | 497 | imagePanel->statusLabel()->setText( tr("No image - select Open from File menu.") ); |
498 | } else { | 498 | } else { |
499 | QString message("%1x%2"); | 499 | QString message("%1x%2"); |
500 | message = message.arg(image.width()).arg(image.height()); | 500 | message = message.arg(image.width()).arg(image.height()); |
501 | if ( pm.size() != pmScaled.size() ) | 501 | if ( pm.size() != pmScaled.size() ) |
502 | message += QString(" [%1x%2]").arg(pmScaled.width()).arg(pmScaled.height()); | 502 | message += QString(" [%1x%2]").arg(pmScaled.width()).arg(pmScaled.height()); |
503 | if (image.valid(pickx,picky)) { | 503 | if (image.valid(pickx,picky)) { |
504 | QString moremsg; | 504 | QString moremsg; |
505 | moremsg.sprintf("(%d,%d)=#%0*x ", | 505 | moremsg.sprintf("(%d,%d)=#%0*x ", |
506 | pickx, picky, | 506 | pickx, picky, |
507 | image.hasAlphaBuffer() ? 8 : 6, | 507 | image.hasAlphaBuffer() ? 8 : 6, |
508 | image.pixel(pickx,picky)); | 508 | image.pixel(pickx,picky)); |
509 | message += moremsg; | 509 | message += moremsg; |
510 | } | 510 | } |
511 | if ( image.numColors() > 0 ) { | 511 | if ( image.numColors() > 0 ) { |
512 | if (image.valid(pickx,picky)) { | 512 | if (image.valid(pickx,picky)) { |
513 | message += tr(", %1/%2 colors") | 513 | message += tr(", %1/%2 colors") |
514 | .arg(image.pixelIndex(pickx,picky)) | 514 | .arg(image.pixelIndex(pickx,picky)) |
515 | .arg(image.numColors()); | 515 | .arg(image.numColors()); |
516 | } else { | 516 | } else { |
517 | message += tr(", %1 colors").arg(image.numColors()); | 517 | message += tr(", %1 colors").arg(image.numColors()); |
518 | } | 518 | } |
519 | } else if ( image.depth() >= 16 ) { | 519 | } else if ( image.depth() >= 16 ) { |
520 | message += tr(" True color"); | 520 | message += tr(" True color"); |
521 | } | 521 | } |
522 | if ( image.hasAlphaBuffer() ) { | 522 | if ( image.hasAlphaBuffer() ) { |
523 | if ( image.depth() == 8 ) { | 523 | if ( image.depth() == 8 ) { |
524 | int i; | 524 | int i; |
525 | bool alpha[256]; | 525 | bool alpha[256]; |
526 | int nalpha=0; | 526 | int nalpha=0; |
527 | 527 | ||
528 | for (i=0; i<256; i++) | 528 | for (i=0; i<256; i++) |
529 | alpha[i] = FALSE; | 529 | alpha[i] = FALSE; |
530 | 530 | ||
531 | for (i=0; i<image.numColors(); i++) { | 531 | for (i=0; i<image.numColors(); i++) { |
532 | int alevel = image.color(i) >> 24; | 532 | int alevel = image.color(i) >> 24; |
533 | if (!alpha[alevel]) { | 533 | if (!alpha[alevel]) { |
534 | alpha[alevel] = TRUE; | 534 | alpha[alevel] = TRUE; |
535 | nalpha++; | 535 | nalpha++; |
536 | } | 536 | } |
537 | } | 537 | } |
538 | message += tr(", %1 alpha levels").arg(nalpha); | 538 | message += tr(", %1 alpha levels").arg(nalpha); |
539 | } else { | 539 | } else { |
540 | // Too many pixels to bother counting. | 540 | // Too many pixels to bother counting. |
541 | message += tr(", 8-bit alpha channel"); | 541 | message += tr(", 8-bit alpha channel"); |
542 | } | 542 | } |
543 | } | 543 | } |
544 | imagePanel->statusLabel()->setText(message); | 544 | imagePanel->statusLabel()->setText(message); |
545 | } | 545 | } |
546 | } | 546 | } |
547 | 547 | ||
548 | void ImageViewer::closeEvent( QCloseEvent *e ) | 548 | void ImageViewer::closeEvent( QCloseEvent *e ) |
549 | { | 549 | { |
550 | if ( stack->visibleWidget() == imagePanel && !bFromDocView ) { | 550 | if ( stack->visibleWidget() == imagePanel && !bFromDocView ) { |
551 | e->ignore(); | 551 | e->ignore(); |
552 | open(); | 552 | open(); |
553 | } else { | 553 | } else { |
554 | bFromDocView = FALSE; | 554 | bFromDocView = FALSE; |
555 | e->accept(); | 555 | e->accept(); |
556 | } | 556 | } |
557 | } | 557 | } |