kmag.cpp 27.7 KB
Newer Older
Sarang Lakare's avatar
Sarang Lakare committed
1 2 3 4
/***************************************************************************
                          kmag.cpp  -  description
                             -------------------
    begin                : Mon Feb 12 23:45:41 EST 2001
5
    copyright            : (C) 2001-2003 by Sarang Lakare
Sarang Lakare's avatar
Sarang Lakare committed
6
    email                : sarang#users.sourceforge.net
7
    copyright            : (C) 2003-2004 by Olaf Schmidt
8
    email                : ojschmidt@kde.org
9 10
    copyright            : (C) 2008 by Matthew Woehlke
    email                : mw_triad@users.sourceforge.net
Sebastian Sauer's avatar
Sebastian Sauer committed
11 12
    copyright              (C) 2010 Sebastian Sauer
    email                  sebsauer@kdab.com
Sarang Lakare's avatar
Sarang Lakare committed
13 14 15 16 17 18 19 20 21 22 23
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

Christoph Feck's avatar
Christoph Feck committed
24
// include files for Qt
25
#include <QApplication>
Christoph Feck's avatar
Christoph Feck committed
26
#include <QClipboard>
Christoph Feck's avatar
Christoph Feck committed
27
#include <QDebug>
Christoph Feck's avatar
Christoph Feck committed
28
#include <QDesktopWidget>
29 30
#include <QFileDialog>
#include <QImageReader>
Christoph Feck's avatar
Christoph Feck committed
31
#include <QMenuBar>
Christoph Feck's avatar
Christoph Feck committed
32
#include <QPainter>
Christoph Feck's avatar
Christoph Feck committed
33
#include <QPrintDialog>
34
#include <QTemporaryFile>
Laurent Montel's avatar
Laurent Montel committed
35

Arto Hytönen's avatar
Arto Hytönen committed
36
// include files for KDE
Christoph Feck's avatar
Christoph Feck committed
37 38 39 40 41 42 43 44 45
#include <KActionCollection>
#include <KConfigGroup>
#include <KEditToolBar>
#include <KLocalizedString>
#include <KMessageBox>
#include <KSelectAction>
#include <KShortcutsDialog>
#include <KToggleAction>
#include <KXMLGUIFactory>
46
#include <KIO/FileCopyJob>
Sarang Lakare's avatar
Sarang Lakare committed
47 48

// application specific includes
Christoph Feck's avatar
Christoph Feck committed
49
#include "kmag.h"
Sarang Lakare's avatar
Sarang Lakare committed
50
#include "kmagzoomview.h"
Sarang Lakare's avatar
Sarang Lakare committed
51 52
#include "kmagselrect.h"

53

54
KmagApp::KmagApp(QWidget* , const char* name)
55 56
  : KXmlGuiWindow(0) // Qt::WStyle_MinMax | Qt::WType_TopLevel | Qt::WDestructiveClose | Qt::WStyle_ContextHelp | Qt::WindowCloseButtonHint | Qt::WStyle_StaysOnTop
  , m_defaultMouseCursorType(2)
Sarang Lakare's avatar
Sarang Lakare committed
57
{
58
  setObjectName( QLatin1String( name ) );
59
  setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
60
  config=KSharedConfig::openConfig();
Sarang Lakare's avatar
Sarang Lakare committed
61

62 63
  zoomArrayString << QLatin1String( "5:1" ) << QLatin1String( "2:1" ) << QLatin1String( "1:1" ) << QLatin1String( "1:1.5" ) << QLatin1String( "1:2" ) << QLatin1String( "1:3" ) << QLatin1String( "1:4" ) << QLatin1String( "1:5" )
    << QLatin1String( "1:6" ) << QLatin1String( "1:7" ) << QLatin1String( "1:8" ) << QLatin1String( "1:12" ) << QLatin1String( "1:16" ) << QLatin1String( "1:20" );
64

Christoph Feck's avatar
Christoph Feck committed
65 66 67 68
  zoomArray << 0.2 <<0.5 << 1.0;
  zoomArray << 1.5 << 2.0 << 3.0;
  zoomArray << 4.0 << 5.0 << 6.0 << 7.0;
  zoomArray << 8.0 << 12.0 << 16.0 << 20.0;
69

70
  fpsArrayString << i18nc("Zoom at very low", "&Very Low") << i18nc("Zoom at low", "&Low") << i18nc("Zoom at medium", "&Medium") << i18nc("Zoom at high", "&High") << i18nc("Zoom at very high", "V&ery High");
Sarang Lakare's avatar
Sarang Lakare committed
71

Christoph Feck's avatar
Christoph Feck committed
72 73 74 75 76
  fpsArray.append(2); // very low
  fpsArray.append(6); // low
  fpsArray.append(10); // medium
  fpsArray.append(15); // high
  fpsArray.append(25); // very high
Sarang Lakare's avatar
Sarang Lakare committed
77

78
  colorArrayString << i18nc("No color-blindness simulation, i.e. 'normal' vision", "&Normal") << i18n("&Protanopia") << i18n("&Deuteranopia") << i18n("&Tritanopia") << i18n("&Achromatopsia");
79

Christoph Feck's avatar
Christoph Feck committed
80 81 82 83 84
  colorArray.append(0);
  colorArray.append(1);
  colorArray.append(2);
  colorArray.append(3);
  colorArray.append(4);
85

86
  rotationArrayString << i18n("&No Rotation (0 Degrees)") << i18n("&Left (90 Degrees)") << i18n("&Upside Down (180 Degrees)") << i18n("&Right (270 Degrees)");
87

Christoph Feck's avatar
Christoph Feck committed
88 89 90 91
  rotationArray.append(0); // no rotation
  rotationArray.append(90); // left
  rotationArray.append(180); // upside down
  rotationArray.append(270); // right
92

Sarang Lakare's avatar
Sarang Lakare committed
93 94 95
  // call inits to invoke all other construction parts
  initView();
  initActions();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
96
  initConnections();
Laurent Montel's avatar
Laurent Montel committed
97

98
  // read options from config file
Sarang Lakare's avatar
Sarang Lakare committed
99 100
  readOptions();

101 102 103
  #ifndef QT_NO_PRINTER
  m_printer = 0;
  #endif // QT_NO_PRINTER
Sarang Lakare's avatar
Sarang Lakare committed
104 105
}

Sarang Lakare's avatar
Sarang Lakare committed
106 107 108
/**
 * Default destructor.
 */
Sarang Lakare's avatar
Sarang Lakare committed
109 110
KmagApp::~KmagApp()
{
111
    m_zoomView->showSelRect(false);
112 113 114 115

    #ifndef QT_NO_PRINTER
    delete m_printer;
    #endif // QT_NO_PRINTER
Sarang Lakare's avatar
Sarang Lakare committed
116 117 118 119
}

void KmagApp::initActions()
{
120
  fileNewWindow = actionCollection()->addAction(QLatin1String( "new_window" ));
121
  fileNewWindow->setIcon(QIcon::fromTheme(QLatin1String( "window-new" )));
Simon Hausmann's avatar
ported  
Simon Hausmann committed
122
  fileNewWindow->setText(i18n("New &Window"));
Laurent Montel's avatar
Laurent Montel committed
123
  connect(fileNewWindow, SIGNAL(triggered(bool)), SLOT(slotFileNewWindow()));
124
  actionCollection()->setDefaultShortcuts(fileNewWindow, KStandardShortcut::openNew());
125
  fileNewWindow->setToolTip(i18n("Open a new KMagnifier window"));
Sarang Lakare's avatar
Sarang Lakare committed
126

127
  refreshSwitch = actionCollection()->addAction(QLatin1String( "start_stop_refresh" ));
128
  refreshSwitch->setIcon(QIcon::fromTheme(QLatin1String( "process-stop" )));
Simon Hausmann's avatar
ported  
Simon Hausmann committed
129
  refreshSwitch->setText(i18n("&Stop"));
Laurent Montel's avatar
Laurent Montel committed
130
  connect(refreshSwitch, SIGNAL(triggered(bool)), SLOT(slotToggleRefresh()));
131
  actionCollection()->setDefaultShortcuts(refreshSwitch, KStandardShortcut::reload());
Sarang Lakare's avatar
Sarang Lakare committed
132
  refreshSwitch->setToolTip(i18n("Click to stop window refresh"));
133 134 135
  refreshSwitch->setWhatsThis(i18n("Clicking on this icon will <b>start</b> / <b>stop</b> "
                                   "updating of the display. Stopping the update will zero the processing power "
                                   "required (CPU usage)"));
136

137
  m_pSnapshot = actionCollection()->addAction(QLatin1String( "snapshot" ));
138
  m_pSnapshot->setIcon(QIcon::fromTheme(QLatin1String( "ksnapshot" )));
Simon Hausmann's avatar
ported  
Simon Hausmann committed
139
  m_pSnapshot->setText(i18n("&Save Snapshot As..."));
Laurent Montel's avatar
Laurent Montel committed
140
  connect(m_pSnapshot, SIGNAL(triggered(bool)), SLOT(saveZoomPixmap()));
141
  actionCollection()->setDefaultShortcuts(m_pSnapshot, KStandardShortcut::save());
142
  m_pSnapshot->setWhatsThis(i18n("Saves the zoomed view to an image file."));
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
143
  m_pSnapshot->setToolTip(i18n("Save image to a file"));
Sarang Lakare's avatar
Sarang Lakare committed
144

Simon Hausmann's avatar
ported  
Simon Hausmann committed
145
  m_pPrint = actionCollection()->addAction(KStandardAction::Print, this, SLOT(slotFilePrint()));
Sarang Lakare's avatar
Sarang Lakare committed
146
  m_pPrint->setWhatsThis(i18n("Click on this button to print the current zoomed view."));
147

Simon Hausmann's avatar
ported  
Simon Hausmann committed
148
  m_pQuit = actionCollection()->addAction(KStandardAction::Quit, this, SLOT(slotFileQuit()));
Laurent Montel's avatar
Laurent Montel committed
149
  m_pQuit->setToolTip(i18n("Quits the application"));
150 151
  m_pQuit->setWhatsThis (i18n("Quits the application"));

Simon Hausmann's avatar
ported  
Simon Hausmann committed
152
  m_pCopy = actionCollection()->addAction(KStandardAction::Copy, this, SLOT(copyToClipBoard()));
Sarang Lakare's avatar
Sarang Lakare committed
153
  m_pCopy->setWhatsThis(i18n("Click on this button to copy the current zoomed view to the clipboard which you can paste in other applications."));
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
154
  m_pCopy->setToolTip(i18n("Copy zoomed image to clipboard"));
Sarang Lakare's avatar
Sarang Lakare committed
155

156 157 158
  m_pShowMenu = KStandardAction::showMenubar(this, SLOT(slotShowMenu()), actionCollection());

  setStandardToolBarMenuEnabled(true);
159

160
  m_modeFollowMouse = new KToggleAction(QIcon::fromTheme(QLatin1String( "followmouse" )), i18n("&Follow Mouse Mode"), this);
161
  actionCollection()->addAction(QLatin1String( "mode_followmouse" ), m_modeFollowMouse);
162
  connect(m_modeFollowMouse, SIGNAL(triggered(bool)), SLOT(slotModeChanged()));
163
  actionCollection()->setDefaultShortcut(m_modeFollowMouse, Qt::Key_F2);
Sebastian Sauer's avatar
Sebastian Sauer committed
164
  m_modeFollowMouse->setIconText(i18n("Mouse"));
165 166
  m_modeFollowMouse->setToolTip(i18n("Magnify around the mouse cursor"));
  m_modeFollowMouse->setWhatsThis(i18n("If selected, the area around the mouse cursor is magnified"));
167

168
#ifdef QAccessibilityClient_FOUND
169

170
  m_modeFollowFocus = new KToggleAction(QIcon::fromTheme(QLatin1String( "view-restore" )), i18n("&Follow Focus Mode"), this);
171
  actionCollection()->addAction(QLatin1String( "mode_followfocus" ), m_modeFollowFocus);
172
  connect(m_modeFollowFocus, SIGNAL(triggered(bool)), SLOT(slotModeChanged()));
173
  actionCollection()->setDefaultShortcut(m_modeFollowFocus, Qt::Key_F2);
Sebastian Sauer's avatar
Sebastian Sauer committed
174
  m_modeFollowFocus->setIconText(i18n("Focus"));
175 176
  m_modeFollowFocus->setToolTip(i18n("Magnify around the keyboard focus"));
  m_modeFollowFocus->setWhatsThis(i18n("If selected, the area around the keyboard cursor is magnified"));
177

178
#endif
179

180
  m_modeSelWin = new KToggleAction(QIcon::fromTheme(QLatin1String( "window" )), i18n("Se&lection Window Mode"), this);
181
  actionCollection()->addAction(QLatin1String( "mode_selectionwindow" ), m_modeSelWin);
Stephan Kulow's avatar
Stephan Kulow committed
182
  connect(m_modeSelWin, SIGNAL(triggered(bool)), SLOT(slotModeSelWin()));
183
  actionCollection()->setDefaultShortcut(m_modeSelWin, Qt::Key_F3);
Sebastian Sauer's avatar
Sebastian Sauer committed
184
  m_modeSelWin->setIconText(i18n("Window"));
185
  m_modeSelWin->setToolTip(i18n("Show a window for selecting the magnified area"));
186

187
  m_modeWholeScreen = new KToggleAction(QIcon::fromTheme(QLatin1String( "view-fullscreen" )), i18n("&Whole Screen Mode"), this);
188
  actionCollection()->addAction(QLatin1String( "mode_wholescreen" ), m_modeWholeScreen);
Stephan Kulow's avatar
Stephan Kulow committed
189
  connect(m_modeWholeScreen, SIGNAL(triggered(bool)), SLOT(slotModeWholeScreen()));
190
  actionCollection()->setDefaultShortcut(m_modeWholeScreen, Qt::Key_F4);
Sebastian Sauer's avatar
Sebastian Sauer committed
191
  m_modeWholeScreen->setIconText(i18n("Screen"));
192 193
  m_modeWholeScreen->setToolTip(i18n("Magnify the whole screen"));
  m_modeWholeScreen->setWhatsThis(i18n("Click on this button to fit the zoom view to the zoom window."));
194

195
  m_hideCursor = new KToggleAction(QIcon::fromTheme(QLatin1String( "hidemouse" )), i18n("Hide Mouse &Cursor"), this);
196
  actionCollection()->addAction(QLatin1String( "hidecursor" ), m_hideCursor);
Stephan Kulow's avatar
Stephan Kulow committed
197
  connect(m_hideCursor, SIGNAL(triggered(bool)), SLOT(slotToggleHideCursor()));
198
  actionCollection()->setDefaultShortcut(m_hideCursor, Qt::Key_F6);
Sebastian Sauer's avatar
Sebastian Sauer committed
199
  #ifdef havesetCheckedStatef
Laurent Montel's avatar
Laurent Montel committed
200
  m_hideCursor->setCheckedState(KGuiItem(i18n("Show Mouse &Cursor")));
201
  #endif
Sebastian Sauer's avatar
Sebastian Sauer committed
202
  m_hideCursor->setIconText(i18n("Hide"));
203
  m_hideCursor->setToolTip(i18n("Hide the mouse cursor"));
204

205
  m_staysOnTop = new KToggleAction(QIcon::fromTheme(QLatin1String( "go-top" )), i18n("Stays On Top"), this);
206
  actionCollection()->addAction(QLatin1String( "staysontop" ), m_staysOnTop);
Sebastian Sauer's avatar
Sebastian Sauer committed
207
  connect(m_staysOnTop, SIGNAL(triggered(bool)), SLOT(slotStaysOnTop()));
208
  actionCollection()->setDefaultShortcut(m_staysOnTop, Qt::Key_F7);
Sebastian Sauer's avatar
Sebastian Sauer committed
209
  m_staysOnTop->setToolTip(i18n("The KMagnifier Window stays on top of other windows."));
210

Simon Hausmann's avatar
ported  
Simon Hausmann committed
211
  m_pZoomIn = actionCollection()->addAction(KStandardAction::ZoomIn, this, SLOT(zoomIn()));
212 213
  m_pZoomIn->setWhatsThis(i18n("Click on this button to <b>zoom-in</b> on the selected region."));

Simon Hausmann's avatar
ported  
Simon Hausmann committed
214
  m_pZoomBox = new KSelectAction(i18n("&Zoom"), this);
215
  actionCollection()->addAction(QLatin1String( "zoom" ), m_pZoomBox);
216
  m_pZoomBox->setItems(zoomArrayString);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
217 218
  m_pZoomBox->setWhatsThis(i18n("Select the zoom factor."));
  m_pZoomBox->setToolTip(i18n("Zoom factor"));
219

Simon Hausmann's avatar
ported  
Simon Hausmann committed
220
  m_pZoomOut = actionCollection()->addAction(KStandardAction::ZoomOut, this, SLOT(zoomOut()));
Sarang Lakare's avatar
Sarang Lakare committed
221
  m_pZoomOut->setWhatsThis(i18n("Click on this button to <b>zoom-out</b> on the selected region."));
222

Simon Hausmann's avatar
ported  
Simon Hausmann committed
223
  m_pRotationBox = new KSelectAction(i18n("&Rotation"),this);
224
  actionCollection()->addAction(QLatin1String( "rotation" ), m_pRotationBox);
225
  m_pRotationBox->setItems(rotationArrayString);
Gunnar Schmidt's avatar
Gunnar Schmidt committed
226 227
  m_pRotationBox->setWhatsThis(i18n("Select the rotation degree."));
  m_pRotationBox->setToolTip(i18n("Rotation degree"));
228

Simon Hausmann's avatar
ported  
Simon Hausmann committed
229 230
  m_keyConf = actionCollection()->addAction(KStandardAction::KeyBindings, this, SLOT(slotConfKeys()));
  m_toolConf = actionCollection()->addAction(KStandardAction::ConfigureToolbars, this, SLOT(slotEditToolbars()));
231

Simon Hausmann's avatar
ported  
Simon Hausmann committed
232
  m_pFPSBox = new KSelectAction(i18n("&Refresh"),this);
233
  actionCollection()->addAction(QLatin1String( "fps_selector" ), m_pFPSBox);
Sarang Lakare's avatar
Sarang Lakare committed
234
  m_pFPSBox->setItems(fpsArrayString);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
235 236
  m_pFPSBox->setWhatsThis(i18n("Select the refresh rate. The higher the rate, the more computing power (CPU) will be needed."));
  m_pFPSBox->setToolTip(i18n("Refresh rate"));
Sarang Lakare's avatar
Sarang Lakare committed
237

238
  m_pColorBox = new KSelectAction(i18nc("Color-blindness simulation mode", "&Color"),this);
239
  actionCollection()->addAction(QLatin1String( "color_mode" ), m_pColorBox);
240 241 242 243
  m_pColorBox->setItems(colorArrayString);
  m_pColorBox->setWhatsThis(i18n("Select a mode to simulate various types of color-blindness."));
  m_pColorBox->setToolTip(i18n("Color-blindness Simulation Mode"));

244
  setupGUI(ToolBar | Keys | Save | Create);
Sarang Lakare's avatar
Sarang Lakare committed
245 246
}

Sarang Lakare's avatar
Sarang Lakare committed
247 248
void KmagApp::initView()
{
249
  m_zoomView = new KMagZoomView( this, "ZoomView" );
250
//m_zoomView->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)7, m_zoomView->sizePolicy().hasHeightForWidth() ) );
251
  m_zoomView->setFrameShape( QFrame::NoFrame );
Sarang Lakare's avatar
Sarang Lakare committed
252

253
  setCentralWidget(m_zoomView);
Sarang Lakare's avatar
Sarang Lakare committed
254 255
}

Laurent Montel's avatar
Laurent Montel committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

void KmagApp::slotChangeZoomBoxIndex(int index)
{
   m_pZoomBox->setCurrentItem(index);
}

void KmagApp::slotChangeRotationBoxIndex(int index)
{
  m_pRotationBox->setCurrentItem(index);
}

void KmagApp::slotChangeFPSIndex(int index)
{
  m_pFPSBox->setCurrentItem(index);
}

272 273 274 275
void KmagApp::slotChangeColorIndex(int index)
{
  m_pColorBox->setCurrentItem(index);
}
Laurent Montel's avatar
Laurent Montel committed
276

Sarang Lakare's avatar
Sarang Lakare committed
277 278 279
/**
 * Initialize all connections.
 */
280 281
void KmagApp::initConnections()
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
282 283
  // change in zoom value -> update the view
  connect(this, SIGNAL(updateZoomValue(float)), m_zoomView, SLOT(setZoom(float)));
284
  connect(this, SIGNAL(updateRotationValue(int)), m_zoomView, SLOT(setRotation(int)));
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
285
  connect(this, SIGNAL(updateFPSValue(float)), m_zoomView, SLOT(setRefreshRate(float)));
286
  connect(this, SIGNAL(updateColorValue(int)), m_zoomView, SLOT(setColorMode(int)));
Sarang Lakare's avatar
Sarang Lakare committed
287

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
288
  // change in zoom index -> update the selector
Laurent Montel's avatar
Laurent Montel committed
289 290 291
  connect(this, SIGNAL(updateZoomIndex(int)), this, SLOT(slotChangeZoomBoxIndex(int)));
  connect(this, SIGNAL(updateRotationIndex(int)), this, SLOT(slotChangeRotationBoxIndex(int)));
  connect(this, SIGNAL(updateFPSIndex(int)), this, SLOT(slotChangeFPSIndex(int)));
292
  connect(this, SIGNAL(updateColorIndex(int)), this, SLOT(slotChangeColorIndex(int)));
Sarang Lakare's avatar
Sarang Lakare committed
293

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
294
  // selector selects a zoom index -> set the zoom index
295 296 297
  connect(m_pZoomBox, SIGNAL(triggered(int)), this, SLOT(setZoomIndex(int)));
  connect(m_pRotationBox, SIGNAL(triggered(int)), this, SLOT(setRotationIndex(int)));
  connect(m_pFPSBox, SIGNAL(triggered(int)), this, SLOT(setFPSIndex(int)));
298
  connect(m_pColorBox, SIGNAL(triggered(int)), this, SLOT(setColorIndex(int)));
299 300
}

301 302 303
/**
 * Save options to config file.
 */
Sarang Lakare's avatar
Sarang Lakare committed
304
void KmagApp::saveOptions()
Laurent Montel's avatar
Laurent Montel committed
305
{
Stephan Kulow's avatar
ported  
Stephan Kulow committed
306 307 308 309 310
  KConfigGroup cg( config, "General Options");
  cg.writeEntry("Geometry", size());
  cg.writeEntry("ZoomIndex", m_zoomIndex);
  cg.writeEntry("RotationIndex", m_rotationIndex);
  cg.writeEntry("FPSIndex", m_fpsIndex);
311
  cg.writeEntry("ColorIndex", m_colorIndex);
Stephan Kulow's avatar
ported  
Stephan Kulow committed
312 313
  cg.writeEntry("SelRect", m_zoomView->getSelRectPos());
  cg.writeEntry("ShowMouse", m_zoomView->getShowMouseType());
Sebastian Sauer's avatar
Sebastian Sauer committed
314
  cg.writeEntry("StaysOnTop", m_staysOnTop->isChecked());
315

316
  if (m_modeFollowMouse->isChecked())
Stephan Kulow's avatar
ported  
Stephan Kulow committed
317
     cg.writeEntry("Mode", "followmouse");
318
#ifdef QAccessibilityClient_FOUND
319 320
  else if (m_modeFollowFocus->isChecked())
     cg.writeEntry("Mode", "followfocus");
321
#endif
322
  else if (m_modeWholeScreen->isChecked())
Stephan Kulow's avatar
ported  
Stephan Kulow committed
323
     cg.writeEntry("Mode", "wholescreen");
324
  else if (m_modeSelWin->isChecked())
Stephan Kulow's avatar
ported  
Stephan Kulow committed
325
     cg.writeEntry("Mode", "selectionwindow");
Sarang Lakare's avatar
Sarang Lakare committed
326 327 328
}


329 330 331
/**
 * Read settings from config file.
 */
Sarang Lakare's avatar
Sarang Lakare committed
332 333
void KmagApp::readOptions()
{
334 335 336 337
  QColor blue (0,0,128);
  QColor yellow (255,255,0);
  QColor white (255,255,255);

Laurent Montel's avatar
Laurent Montel committed
338
  KConfigGroup cgWM( config, "WM");
339
  setTitleColors (
Laurent Montel's avatar
Laurent Montel committed
340 341 342
      cgWM.readEntry("inactiveBackground", blue),
      cgWM.readEntry("inactiveForeground", white),
      cgWM.readEntry("inactiveTitleBtnBg", yellow));
343

Laurent Montel's avatar
Laurent Montel committed
344
  KConfigGroup cg(config,"General Options");
Sarang Lakare's avatar
Sarang Lakare committed
345

346
  QSize defSize(460,390);
Stephan Kulow's avatar
ported  
Stephan Kulow committed
347
  QSize size=cg.readEntry("Geometry", defSize);
Sarang Lakare's avatar
Sarang Lakare committed
348 349 350 351 352
  if(!size.isEmpty())
  {
    resize(size);
  }

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
353
  // set zoom - defaults to 2x
Stephan Kulow's avatar
ported  
Stephan Kulow committed
354
  unsigned int zoomIndex = cg.readEntry("ZoomIndex", 4);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
355 356
  setZoomIndex(zoomIndex);
  emit updateZoomIndex(m_zoomIndex);
Sarang Lakare's avatar
Sarang Lakare committed
357

Stephan Kulow's avatar
ported  
Stephan Kulow committed
358
  unsigned int rotationIndex = cg.readEntry("RotationIndex", 0);
359 360 361
  setRotationIndex(rotationIndex);
  emit updateRotationIndex(m_rotationIndex);

Stephan Kulow's avatar
ported  
Stephan Kulow committed
362
  unsigned int fpsIndex = cg.readEntry("FPSIndex", 2);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
363 364
  setFPSIndex(fpsIndex);
  emit updateFPSIndex(m_fpsIndex);
Sarang Lakare's avatar
Sarang Lakare committed
365

366 367 368
  unsigned int colorIndex = cg.readEntry("ColorIndex", 0);
  setColorIndex(colorIndex);
  emit updateColorIndex(colorIndex);
369
/*
Stephan Kulow's avatar
ported  
Stephan Kulow committed
370
  QString mode = cg.readEntry("Mode", "followmouse");
371
  if (mode == QLatin1String( "wholescreen" )) {
372
    slotModeWholeScreen();
373
  } else if (mode == QLatin1String( "selectionwindow" )) {
374
    slotModeSelWin();
375 376 377 378 379 380 381 382
  } else if (mode == QLatin1String( "followfocus" )) {
    m_modeFollowFocus->setChecked(true);
    slotModeChanged();
  } else {
    m_modeFollowMouse->setChecked(true);
    slotModeChanged();
  }
*/
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
383
  QRect defaultRect(0,0,211,164);
Stephan Kulow's avatar
ported  
Stephan Kulow committed
384
  m_zoomView->setSelRectPos(cg.readEntry("SelRect", defaultRect));
385

Stephan Kulow's avatar
ported  
Stephan Kulow committed
386
  m_mouseCursorType = cg.readEntry("ShowMouse", m_defaultMouseCursorType);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
387
  m_zoomView->showMouse(m_mouseCursorType);
Sebastian Sauer's avatar
Sebastian Sauer committed
388
  m_hideCursor->setChecked(!m_mouseCursorType);
389

Sebastian Sauer's avatar
Sebastian Sauer committed
390 391
  m_staysOnTop->setChecked(cg.readEntry("StaysOnTop", true));
  slotStaysOnTop();
Stephan Binner's avatar
Stephan Binner committed
392

393 394
  // XMLGui has already read and set up the menuBar for us
  m_pShowMenu->setChecked(menuBar()->isVisible());
395
}
Sarang Lakare's avatar
Sarang Lakare committed
396 397 398 399

bool KmagApp::queryClose()
{
  saveOptions();
400
  return (true);
Sarang Lakare's avatar
Sarang Lakare committed
401 402
}

403 404 405 406 407
/**
 * Called when mouse is clicked inside the window
 *
 * @param e
 */
408
void KmagApp::contextMenuEvent ( QContextMenuEvent * e )
409
{
410 411
 // show popup
 KXMLGUIFactory *factory = this->factory();
412
 QMenu *popup = (QMenu *)factory->container(QLatin1String( "mainPopUp" ),this);
413
 if (popup != 0)
414
   popup->popup(e->globalPos());
415
 e->accept();
416 417 418
}


Sarang Lakare's avatar
Sarang Lakare committed
419 420 421 422
/////////////////////////////////////////////////////////////////////
// SLOT IMPLEMENTATION
/////////////////////////////////////////////////////////////////////

Sarang Lakare's avatar
Sarang Lakare committed
423 424 425 426 427
/**
 * Shows/hides the mouse cursor
 */
void KmagApp::showMouseCursor(bool show)
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
428
  if(show) {
Sarang Lakare's avatar
Sarang Lakare committed
429 430
    if(m_mouseCursorType == 0)
      m_mouseCursorType = m_defaultMouseCursorType;
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
431 432 433 434
    m_zoomView->showMouse(m_mouseCursorType);
  } else {
    m_zoomView->showMouse(0);
  }
Sarang Lakare's avatar
Sarang Lakare committed
435 436
}

437 438 439 440
/**
 * Zoom in.
 */
void KmagApp::zoomIn()
441
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
442 443 444 445
  // set the new index .. checking will done inside setZoom
  setZoomIndex(m_zoomIndex+1);
  // signal change in zoom index
  emit updateZoomIndex((int)m_zoomIndex);
446 447
}

448 449 450 451
/**
 * Zoom out.
 */
void KmagApp::zoomOut()
452
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
453 454 455 456
  // set the new index .. checking will done inside setZoom
  setZoomIndex(m_zoomIndex-1);
  // signal change in zoom index
  emit updateZoomIndex((int)m_zoomIndex);
457
}
458 459 460 461
/**
 * Sets the zoom index to index
 */
void KmagApp::setZoomIndex(int index)
462
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
463 464
  if(index < 0 || index >= (int)zoomArray.size()) {
    // the index is invalid
Christoph Feck's avatar
Christoph Feck committed
465
    qDebug() << "Invalid index!" ;
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
466
    return;
467
  } else if((int)m_zoomIndex == index) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
468 469 470 471 472
    // do nothing!
    return;
  } else {
    m_zoomIndex = index;
  }
473 474 475 476 477 478

  if(m_zoomIndex == 0) {
    // meaning that no more zooming-out is possible
    // -> disable zoom-out icon
    m_pZoomOut->setEnabled(false);
  } else { // enable the icon
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
479 480
    m_pZoomOut->setEnabled(true);
  }
481

482
  if(m_zoomIndex == zoomArray.size()-1) {
483 484
    // meaning that no more zooming-in is possible
    // -> disable zoom-in icon
485 486
    m_pZoomIn->setEnabled(false);
  } else { // enable the icon
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
487 488
    m_pZoomIn->setEnabled(true);
  }
Laurent Montel's avatar
Laurent Montel committed
489

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
490
  // signal change in zoom value
Christoph Feck's avatar
Christoph Feck committed
491
  emit updateZoomValue(zoomArray.at(m_zoomIndex));
492 493
}

494 495 496 497 498 499 500
/**
 * Sets the rotation index to index
 */
void KmagApp::setRotationIndex(int index)
{
  if(index < 0 || index >= (int)rotationArray.size()) {
    // the index is invalid
Christoph Feck's avatar
Christoph Feck committed
501
    qDebug() << "Invalid index!" ;
502 503 504 505 506 507 508 509 510
    return;
  } else if((int)m_rotationIndex == index) {
    // do nothing!
    return;
  } else {
    m_rotationIndex = index;
  }

  // signal change in zoom value
Christoph Feck's avatar
Christoph Feck committed
511
  emit updateRotationValue(rotationArray.at(m_rotationIndex));
512 513
}

Sarang Lakare's avatar
Sarang Lakare committed
514 515 516 517 518
/**
 * Sets the fps index to index
 */
void KmagApp::setFPSIndex(int index)
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
519 520
  if(index < 0 || index >= (int)fpsArray.size()) {
    // the index is invalid
Christoph Feck's avatar
Christoph Feck committed
521
    qDebug() << "Invalid index!" ;
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
522
    return;
Sarang Lakare's avatar
Sarang Lakare committed
523
  } else if((int)m_fpsIndex == index) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
524 525 526 527 528 529
    // do nothing!
    return;
  } else {
    m_fpsIndex = index;
  }

530
  // signal change in fps value
Christoph Feck's avatar
Christoph Feck committed
531
  emit updateFPSValue(fpsArray.at(m_fpsIndex));
Sarang Lakare's avatar
Sarang Lakare committed
532 533
}

534 535 536 537 538 539 540
/**
 * Sets the color index to index
 */
void KmagApp::setColorIndex(int index)
{
  if(index < 0 || index >= (int)colorArray.size()) {
    // the index is invalid
Christoph Feck's avatar
Christoph Feck committed
541
    qDebug() << "Invalid index!" ;
542 543 544 545 546 547 548 549 550
    return;
  } else if((int)m_colorIndex == index) {
    // do nothing!
    return;
  } else {
    m_colorIndex = index;
  }

  // signal change in fps value
Christoph Feck's avatar
Christoph Feck committed
551
  emit updateColorValue(colorArray.at(m_colorIndex));
552
}
553

Sarang Lakare's avatar
Sarang Lakare committed
554 555 556 557 558
/**
 * Save the zoomed image
 */
void KmagApp::saveZoomPixmap()
{
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
559
  bool toggled(false);
Sarang Lakare's avatar
Sarang Lakare committed
560

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
561
  // stop refresh temporarily
Sarang Lakare's avatar
Sarang Lakare committed
562 563
  if (m_zoomView->getRefreshStatus()) {
    slotToggleRefresh();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
564
    toggled = true;
Sarang Lakare's avatar
Sarang Lakare committed
565 566
  }

567 568 569 570 571 572 573 574 575 576 577 578 579
  QStringList mimeTypes;
  QList<QByteArray> supported = QImageReader::supportedMimeTypes();
  foreach (QByteArray mimeType, supported) {
    mimeTypes.append(QString::fromLatin1(mimeType));
  }

  QFileDialog fileDialog(this);
  fileDialog.setWindowTitle(i18n("Save Snapshot As"));
  fileDialog.setMimeTypeFilters(mimeTypes);
  QUrl url;
  if (fileDialog.exec() && !fileDialog.selectedUrls().isEmpty()) {
      url = fileDialog.selectedUrls().at(0);
  }
Sarang Lakare's avatar
Sarang Lakare committed
580

581
  if(!url.fileName().isEmpty()) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
582 583
    if(!url.isLocalFile()) {
      // create a temp file.. save image to it.. copy over the n/w and then delete the temp file.
584
      QTemporaryFile tempFile;
585
#ifdef __GNUC__
Laurent Montel's avatar
Laurent Montel committed
586
#warning "kde4: port KImageIO::type \n";
Stephan Kulow's avatar
ported  
Stephan Kulow committed
587
#endif
588
      if(!tempFile.open() || !m_zoomView->getImage().save(tempFile.fileName(),"png"/*, KImageIO::type(url.fileName()).latin1()*/)) {
589
        KMessageBox::error(0, i18n("Unable to save temporary file (before uploading to the network file you specified)."),
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
590 591
                          i18n("Error Writing File"));
      } else {
592 593
        KIO::FileCopyJob *job = KIO::file_copy(QUrl::fromLocalFile(tempFile.fileName()), url);
        if(!job) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
594 595 596
          KMessageBox::error(0, i18n("Unable to upload file over the network."),
                            i18n("Error Writing File"));
        } else {
Christoph Feck's avatar
Christoph Feck committed
597
          KMessageBox::information(0, i18n("Current zoomed image saved to\n%1", url.toDisplayString()),
598
                              i18n("Information"), QLatin1String( "save_confirm" ));
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
599 600 601 602
        }
      }

    } else {
603
#ifdef __GNUC__
Laurent Montel's avatar
Laurent Montel committed
604
#warning "kde4 : port KImageIO::type(...) \n";
Stephan Kulow's avatar
ported  
Stephan Kulow committed
605
#endif
606
      if(!m_zoomView->getImage().save(url.path(), "png"/*KImageIO::type(url.fileName()).latin1()*/)) {
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
607 608 609
        KMessageBox::error(0, i18n("Unable to save file. Please check if you have permission to write to the directory."),
                            i18n("Error Writing File"));
      } else {
Christoph Feck's avatar
Christoph Feck committed
610
        KMessageBox::information(0, i18n("Current zoomed image saved to\n%1", url.toDisplayString()),
611
                                i18n("Information"), QLatin1String( "save_confirm" ));
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
612 613 614 615 616 617
      }
    }
  }
  if(toggled) {
    slotToggleRefresh();
  }
Sarang Lakare's avatar
Sarang Lakare committed
618 619 620
}


621 622
void KmagApp::slotToggleRefresh()
{
Sarang Lakare's avatar
Sarang Lakare committed
623 624
  m_zoomView->toggleRefresh();
  if(m_zoomView->getRefreshStatus()) {
625
    refreshSwitch->setIcon(QIcon::fromTheme(QLatin1String( "process-stop" )));
626
    refreshSwitch->setText(i18n("Stop"));
Sarang Lakare's avatar
Sarang Lakare committed
627
    refreshSwitch->setToolTip(i18n("Click to stop window update"));
628
  } else {
629
    refreshSwitch->setIcon(QIcon::fromTheme(QLatin1String( "system-run" )));
630
    refreshSwitch->setText(i18nc("Start updating the window", "Start"));
Sarang Lakare's avatar
Sarang Lakare committed
631
    refreshSwitch->setToolTip(i18n("Click to start window update"));
632 633 634
  }
}

635 636

void KmagApp::slotModeWholeScreen()
637
{
638
  m_zoomView->followMouse(false);
639
  m_zoomView->setSelRectPos(QRect (0, 0, QApplication::desktop()->width(), QApplication::desktop()->height()));
640 641 642
  m_zoomView->showSelRect(false);
  m_zoomView->setFitToWindow (false);
  m_modeFollowMouse->setChecked(false);
643
#ifdef QAccessibilityClient_FOUND
644
  m_zoomView->followBoth(false);
645
  m_zoomView->followFocus(false);
646
  m_modeFollowFocus->setChecked(false);
647
#endif
648 649
  m_modeWholeScreen->setChecked(true);
  m_modeSelWin->setChecked(false);
650 651
}

652 653

void KmagApp::slotModeSelWin()
654
{
655 656 657 658
  m_zoomView->followMouse(false);
  m_zoomView->showSelRect(true);
  m_zoomView->setFitToWindow (false);
  m_modeFollowMouse->setChecked(false);
659
#ifdef QAccessibilityClient_FOUND
660
  m_zoomView->followBoth(false);
661
  m_zoomView->followFocus(false);
662
  m_modeFollowFocus->setChecked(false);
663
#endif
664 665
  m_modeWholeScreen->setChecked(false);
  m_modeSelWin->setChecked(true);
666 667
}

668
void KmagApp::slotModeChanged()
669
{
670
#ifdef QAccessibilityClient_FOUND
671 672 673 674 675 676 677 678 679 680 681 682
  if(m_modeFollowMouse->isChecked() && m_modeFollowFocus->isChecked()) {

    //BOTH MODE
    m_zoomView->followMouse(false);
    m_zoomView->followBoth(true);
    m_zoomView->followFocus(false);
    m_zoomView->showSelRect(false);
    m_zoomView->setFitToWindow (true);
    m_modeWholeScreen->setChecked(false);
    m_modeSelWin->setChecked(false);
    return;
  }
683
#endif
684

685
  if (m_modeFollowMouse->isChecked()) {
686

687 688 689 690 691
    //MOUSE MODE
    m_zoomView->followMouse(true);
    m_zoomView->showSelRect(false);
    m_zoomView->setFitToWindow (true);
    m_modeFollowMouse->setChecked(true);
692
#ifdef QAccessibilityClient_FOUND
693 694 695
    m_zoomView->followBoth(false);
    m_zoomView->followFocus(false);
    m_modeFollowFocus->setChecked(false);
696
#endif
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    m_modeWholeScreen->setChecked(false);
    m_modeSelWin->setChecked(false);

#ifdef QAccessibilityClient_FOUND
  } else if (m_modeFollowFocus->isChecked()) {

    //FOCUS MODE
    m_zoomView->followBoth(false);
    m_zoomView->followMouse(false);
    m_zoomView->followFocus(true);
    m_zoomView->showSelRect(false);
    m_zoomView->setFitToWindow (true);
    m_modeFollowMouse->setChecked(false);
    m_modeFollowFocus->setChecked(true);
    m_modeWholeScreen->setChecked(false);
    m_modeSelWin->setChecked(false);
#endif

  }
}
717 718

void KmagApp::slotToggleHideCursor()
719
{
720
  showMouseCursor(!m_hideCursor->isChecked());
721 722
}

723

Sebastian Sauer's avatar
Sebastian Sauer committed
724 725 726
void KmagApp::slotStaysOnTop()
{
  if (m_staysOnTop->isChecked())
727
      setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
Sebastian Sauer's avatar
Sebastian Sauer committed
728
  else
729
      setWindowFlags( windowFlags() & !Qt::WindowStaysOnTopHint );
Sebastian Sauer's avatar
Sebastian Sauer committed
730 731 732 733
  show();
}


Sarang Lakare's avatar
Sarang Lakare committed
734
void KmagApp::slotFileNewWindow()
Laurent Montel's avatar
Laurent Montel committed
735
{
Sarang Lakare's avatar
Sarang Lakare committed
736 737 738 739 740 741 742
  KmagApp *new_window= new KmagApp();
  new_window->show();
}


void KmagApp::slotFilePrint()
{
743 744
#ifndef QT_NO_PRINTER

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
745
  bool toggled(false);
746

747 748 749
  if (m_printer == 0) {
    m_printer = new QPrinter();
  }
750

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
751
  // stop refresh temporarily
752 753
  if (m_zoomView->getRefreshStatus()) {
    slotToggleRefresh();
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
754
    toggled = true;
Sarang Lakare's avatar
Sarang Lakare committed
755
  }
756

757
  const QImage pixmap(m_zoomView->getImage());
Sarang Lakare's avatar
Sarang Lakare committed
758

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
759 760
  // use some AI to get the best orientation
  if(pixmap.width() > pixmap.height()) {
761
    m_printer->setOrientation(QPrinter::Landscape);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
762
  } else {
763
    m_printer->setOrientation(QPrinter::Portrait);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
764
  }
Sarang Lakare's avatar
Sarang Lakare committed
765

766
  QPrintDialog *printDialog = new QPrintDialog(m_printer, this);
John Layt's avatar
John Layt committed
767

768
  if (printDialog->exec()) {
769
    QPainter paint;
Laurent Montel's avatar
Laurent Montel committed
770

771
    if(!paint.begin(m_printer))
Laurent Montel's avatar
Laurent Montel committed
772 773
    {
      delete printDialog;
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
774
      return;
Laurent Montel's avatar
Laurent Montel committed
775
    }
776
    // draw the pixmap
777
    paint.drawImage(0, 0, pixmap);
Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
778 779
    // end the painting
    paint.end();
780 781
  }

Olaf Schmidt-Wischhöfer's avatar
Olaf Schmidt-Wischhöfer committed
782 783 784
  if(toggled) {
    slotToggleRefresh();
  }
Laurent Montel's avatar
Laurent Montel committed
785
  delete printDialog;
786
#endif // QT_NO_PRINTER
Sarang Lakare's avatar
Sarang Lakare committed
787 788
}

789 790 791 792 793 794
void KmagApp::slotFileQuit()
{
  saveOptions();
  // close the first window, the list makes the next one the first again.
  // This ensures that queryClose() is called on each window to ask for closing
  KMainWindow* w;
Laurent Montel's avatar
Laurent Montel committed
795
  if (!memberList().isEmpty())
796
  {
Laurent Montel's avatar
Laurent Montel committed
797
	for (int i = 0; i < memberList().size(); ++i)
798
    {
Laurent Montel's avatar
Laurent Montel committed
799
	  w = memberList().at(i);
800 801 802 803
      // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
      // the window and the application stay open.
      if(!w->close())
         break;
804
#ifdef __GNUC__