summaryrefslogtreecommitdiffstats
path: root/kmag/kmag.cpp
blob: 3c4fd668de50c3e4d255c864fc773b9a8a2e28ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/***************************************************************************
                          kmag.cpp  -  description
                             -------------------
    begin                : Mon Feb 12 23:45:41 EST 2001
    copyright            : (C) 2001-2003 by Sarang Lakare
    email                : sarang#users.sourceforge.net
    copyright            : (C) 2003-2004 by Olaf Schmidt
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// include files for QT
#include <tqdir.h>
#include <tqprinter.h>
#include <tqpainter.h>
#include <tqlayout.h>
#include <tqclipboard.h>
#include <tqdragobject.h>
#include <tqwhatsthis.h>
#include <tqtooltip.h>
#include <tqpopupmenu.h>

#include <tdeversion.h>

// include files for KDE
#if TDE_VERSION > 300
#include <tdeapplication.h>
#else
#include <kapp.h>
#endif // KDE 3.x

#include <kkeydialog.h>
#include <kinputdialog.h>
#include <kiconloader.h>
#include <tdemessagebox.h>
#include <tdefiledialog.h>
#include <tdemenubar.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kstdaction.h>
#include <khelpmenu.h>
#include <kimageio.h>
#include <tdeio/job.h>
#include <tdeio/netaccess.h>
#include <tdetempfile.h>
#include <tdepopupmenu.h>
#include <kedittoolbar.h>
#include <twin.h>

#if TDE_VERSION < 220
#include <tqprinter.h>
#else
#include <kprinter.h>
#endif

// application specific includes
#include "kmag.moc"
#include "kmagzoomview.h"
#include "kmagselrect.h"


#ifdef KDE_IS_VERSION
#if KDE_IS_VERSION(3,2,90)
   #define havesetCheckedState
#endif
#endif

KmagApp::KmagApp(TQWidget* , const char* name)
  : TDEMainWindow(0, name, WStyle_MinMax | WType_TopLevel | WStyle_StaysOnTop | WDestructiveClose | WStyle_ContextHelp),
    m_defaultMouseCursorType(2)
{
  config=kapp->config();

  zoomArrayString << "5:1" << "2:1" << "1:1" << "1:1.5" << "1:2" << "1:3" << "1:4" << "1:5"
    << "1:6" << "1:7" << "1:8" << "1:12" << "1:16" << "1:20";

  // Is there a better way to initialize a vector array?
  zoomArray.push_back(0.2); zoomArray.push_back(0.5); zoomArray.push_back(1.0);
  zoomArray.push_back(1.5); zoomArray.push_back(2.0); zoomArray.push_back(3.0);
  zoomArray.push_back(4.0); zoomArray.push_back(5.0); zoomArray.push_back(6.0); zoomArray.push_back(7.0);
  zoomArray.push_back(8.0); zoomArray.push_back(12.0); zoomArray.push_back(16.0); zoomArray.push_back(20.0);

  fpsArrayString << i18n("&Very Low") << i18n("&Low") << i18n("&Medium") << i18n("&High") << i18n("V&ery High");

  fpsArray.push_back(2); // very low
  fpsArray.push_back(6); // low
  fpsArray.push_back(10); // medium
  fpsArray.push_back(15); // high
  fpsArray.push_back(25); // very high

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

  rotationArray.push_back(0); // no rotation
  rotationArray.push_back(90); // left
  rotationArray.push_back(180); // upside down
  rotationArray.push_back(270); // right

  // call inits to invoke all other construction parts
  initView();
  initActions();
  initConnections();

  // Register all TDEIO image formats - to be used when saving image.
  KImageIO::registerFormats();

  // read options from config file
  readOptions();
}

/**
 * Default destructor.
 */
KmagApp::~KmagApp()
{
    m_zoomView->showSelRect(false);
}

void KmagApp::initActions()
{
  fileNewWindow = new TDEAction(i18n("New &Window"), "window-new", TDEStdAccel::openNew(), this,
                              TQ_SLOT(slotFileNewWindow()), actionCollection(),"new_window");
  fileNewWindow->setToolTip(i18n("Open a new KMagnifier window"));

  refreshSwitch = new TDEAction(i18n("&Stop"), "process-stop", TDEStdAccel::reload(), this,
                              TQ_SLOT(slotToggleRefresh()), actionCollection(), "start_stop_refresh");
  refreshSwitch->setToolTip(i18n("Click to stop window refresh"));
  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)"));

  m_pSnapshot = new TDEAction(i18n("&Save Snapshot As..."), "ksnapshot", TDEStdAccel::save(), this,
                            TQ_SLOT(saveZoomPixmap()), actionCollection(),"snapshot");
  m_pSnapshot->setWhatsThis(i18n("Saves the zoomed view to an image file."));
  m_pSnapshot->setToolTip(i18n("Save image to a file"));

  m_pPrint = KStdAction::print(this, TQ_SLOT(slotFilePrint()), actionCollection(), "print");
  m_pPrint->setWhatsThis(i18n("Click on this button to print the current zoomed view."));

  m_pQuit = KStdAction::quit(this, TQ_SLOT(slotFileQuit()), actionCollection(), "quit");
  m_pQuit->setStatusText(i18n("Quits the application"));
  m_pQuit->setWhatsThis (i18n("Quits the application"));

  m_pCopy = KStdAction::copy(this, TQ_SLOT(copyToClipBoard()), actionCollection(), "copy");
  m_pCopy->setWhatsThis(i18n("Click on this button to copy the current zoomed view to the clipboard which you can paste in other applications."));
  m_pCopy->setToolTip(i18n("Copy zoomed image to clipboard"));

  m_pShowMenu = new TDEToggleAction(i18n("Show &Menu"), "showmenu", CTRL+Key_M, this,
                            TQ_SLOT(slotShowMenu()), actionCollection(),"show_menu");
  #ifdef havesetCheckedState
  m_pShowMenu->setCheckedState(i18n("Hide &Menu"));
  #endif
  m_pShowMainToolBar = new TDEToggleAction(i18n("Show Main &Toolbar"), 0, 0, this,
                            TQ_SLOT(slotShowMainToolBar()), actionCollection(),"show_mainToolBar");
  #ifdef havesetCheckedState
  m_pShowMainToolBar->setCheckedState(i18n("Hide Main &Toolbar"));
  #endif
  m_pShowViewToolBar = new TDEToggleAction(i18n("Show &View Toolbar"), 0, 0, this,
                            TQ_SLOT(slotShowViewToolBar()), actionCollection(),"show_viewToolBar");
  #ifdef havesetCheckedState
  m_pShowViewToolBar->setCheckedState(i18n("Hide &View Toolbar"));
  #endif
  m_pShowSettingsToolBar = new TDEToggleAction(i18n("Show &Settings Toolbar"), 0, 0, this,
                            TQ_SLOT(slotShowSettingsToolBar()), actionCollection(),"show_settingsToolBar");
  #ifdef havesetCheckedState
  m_pShowSettingsToolBar->setCheckedState(i18n("Hide &Settings Toolbar"));
  #endif

  m_modeFollowMouse = new TDERadioAction(i18n("&Follow Mouse Mode"), "followmouse", Key_F1, this,
                            TQ_SLOT(slotModeFollowMouse()), actionCollection(), "mode_followmouse");
  m_modeFollowMouse->setToolTip(i18n("Magnify mouse area into window"));
  m_modeFollowMouse->setWhatsThis(i18n("In this mode the area around the mouse cursor is shown in a normal window."));

  m_modeSelWin = new TDERadioAction(i18n("S&election Window Mode"), "window", Key_F2, this,
                            TQ_SLOT(slotModeSelWin()), actionCollection(), "mode_selectionwindow");
  m_modeSelWin->setToolTip(i18n("Magnify selected area into window"));
  m_modeSelWin->setWhatsThis(i18n("In this mode a selection window is opened. The selected area is shown in a normal window."));

  m_modeEdgeTop = new TDERadioAction(i18n("&Top Screen Edge Mode"), 0, 0, this,
                              TQ_SLOT(slotModeEdgeTop()), actionCollection(),"mode_edgetop");
  m_modeEdgeTop->setToolTip(i18n("Magnify mouse area to top screen edge"));
  m_modeEdgeTop->setWhatsThis(i18n("In this mode the area around the mouse is magnified to the top screen edge."));

  m_modeEdgeLeft = new TDERadioAction(i18n("&Left Screen Edge Mode"), 0, 0, this,
                              TQ_SLOT(slotModeEdgeLeft()), actionCollection(),"mode_edgeleft");
  m_modeEdgeLeft->setToolTip(i18n("Magnify mouse area to left screen edge"));
  m_modeEdgeLeft->setWhatsThis(i18n("In this mode the area around the mouse is magnified to the left screen edge."));

  m_modeEdgeRight = new TDERadioAction(i18n("&Right Screen Edge Mode"), 0, 0, this,
                              TQ_SLOT(slotModeEdgeRight()), actionCollection(),"mode_edgeright");
  m_modeEdgeRight->setToolTip(i18n("Magnify mouse area to right screen edge"));
  m_modeEdgeRight->setWhatsThis(i18n("In this mode the area around the mouse is magnified to the right screen edge."));

  m_modeEdgeBottom = new TDERadioAction(i18n("&Bottom Screen Edge Mode"), 0, 0, this,
                              TQ_SLOT(slotModeEdgeBottom()), actionCollection(),"mode_edgebottom");
  m_modeEdgeBottom->setToolTip(i18n("Magnify mouse area to bottom screen edge"));
  m_modeEdgeBottom->setWhatsThis(i18n("In this mode the area around the mouse is magnified to the bottom screen edge."));

  m_hideCursor = new TDEToggleAction(i18n("Hide Mouse &Cursor"), "hidemouse", Key_F4, this,
                            TQ_SLOT(slotToggleHideCursor()), actionCollection(), "hidecursor");
  #ifdef havesetCheckedState
  m_hideCursor->setCheckedState(i18n("Show Mouse &Cursor"));
  #endif
  m_hideCursor->setToolTip(i18n("Hide the mouse cursor"));

  m_pZoomIn = KStdAction::zoomIn(this, TQ_SLOT(zoomIn()), actionCollection(), "zoom_in");
  m_pZoomIn->setWhatsThis(i18n("Click on this button to <b>zoom-in</b> on the selected region."));

  m_pZoomBox = new TDESelectAction(i18n("&Zoom"),0,actionCollection(),"zoom");
  m_pZoomBox->setItems(zoomArrayString);
  m_pZoomBox->setWhatsThis(i18n("Select the zoom factor."));
  m_pZoomBox->setToolTip(i18n("Zoom factor"));

  m_pZoomOut = KStdAction::zoomOut(this, TQ_SLOT(zoomOut()), actionCollection(), "zoom_out");
  m_pZoomOut->setWhatsThis(i18n("Click on this button to <b>zoom-out</b> on the selected region."));

  m_pInvert = new TDEToggleAction(i18n("&Invert Colors"), 0, Key_F6, this,
                            TQ_SLOT(slotToggleInvert()), actionCollection(), "invert");

  m_pRotationBox = new TDESelectAction(i18n("&Rotation"),0,actionCollection(),"rotation");
  m_pRotationBox->setItems(rotationArrayString);
  m_pRotationBox->setWhatsThis(i18n("Select the rotation degree."));
  m_pRotationBox->setToolTip(i18n("Rotation degree"));

  // KHelpMenu *newHelpMenu = new KHelpMenu(this, TDEGlobal::instance()->aboutData());

  m_keyConf = KStdAction::keyBindings( this, TQ_SLOT( slotConfKeys() ), actionCollection(), "key_conf");
  m_toolConf = KStdAction::configureToolbars( this, TQ_SLOT( slotEditToolbars() ),
                                              actionCollection(), "toolbar_conf");

  m_pFPSBox = new TDESelectAction(i18n("Re&fresh"),0,actionCollection(),"fps_selector");
  m_pFPSBox->setItems(fpsArrayString);
  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"));

  createGUI();
}

void KmagApp::initView()
{
  m_zoomView = new KMagZoomView( this, "ZoomView" );
  m_zoomView->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)7, (TQSizePolicy::SizeType)7, m_zoomView->sizePolicy().hasHeightForWidth() ) );
  m_zoomView->setFrameShape( TQFrame::StyledPanel );
  m_zoomView->setFrameShadow( TQFrame::Raised );

  setCentralWidget(m_zoomView);
}

/**
 * Initialize all connections.
 */
void KmagApp::initConnections()
{
  // change in zoom value -> update the view
  connect(this, TQ_SIGNAL(updateZoomValue(float)), m_zoomView, TQ_SLOT(setZoom(float)));
  connect(this, TQ_SIGNAL(updateRotationValue(int)), m_zoomView, TQ_SLOT(setRotation(int)));
  connect(this, TQ_SIGNAL(updateFPSValue(float)), m_zoomView, TQ_SLOT(setRefreshRate(float)));

  // change in zoom index -> update the selector
  connect(this, TQ_SIGNAL(updateZoomIndex(int)), m_pZoomBox, TQ_SLOT(setCurrentItem(int)));
  connect(this, TQ_SIGNAL(updateRotationIndex(int)), m_pRotationBox, TQ_SLOT(setCurrentItem(int)));
  connect(this, TQ_SIGNAL(updateFPSIndex(int)), m_pFPSBox, TQ_SLOT(setCurrentItem(int)));

  // selector selects a zoom index -> set the zoom index
  connect(m_pZoomBox, TQ_SIGNAL(activated(int)), this, TQ_SLOT(setZoomIndex(int)));
  connect(m_pRotationBox, TQ_SIGNAL(activated(int)), this, TQ_SLOT(setRotationIndex(int)));
  connect(m_pFPSBox, TQ_SIGNAL(activated(int)), this, TQ_SLOT(setFPSIndex(int)));

  connect(m_zoomView, TQ_SIGNAL(contextMenu(TQPoint)), this, TQ_SLOT(contextMenu(TQPoint)));
}

/**
 * Save options to config file.
 */
void KmagApp::saveOptions()
{
  config->setGroup("General Options");
  config->writeEntry("Geometry", size());
  config->writeEntry("ZoomIndex", m_zoomIndex);
  config->writeEntry("Invertation", m_pInvert->isChecked());
  config->writeEntry("RotationIndex", m_rotationIndex);
  config->writeEntry("FPSIndex", m_fpsIndex);
  config->writeEntry("SelRect", m_zoomView->getSelRectPos());
  config->writeEntry("ShowMouse", m_zoomView->getShowMouseType());

  if (m_modeFollowMouse->isChecked())
     config->writeEntry("Mode", "followmouse");
  else if (m_modeSelWin->isChecked())
     config->writeEntry("Mode", "selectionwindow");
  else if (m_modeEdgeTop->isChecked())
     config->writeEntry("Mode", "edgetop");
  else if (m_modeEdgeLeft->isChecked())
     config->writeEntry("Mode", "edgeleft");
  else if (m_modeEdgeRight->isChecked())
     config->writeEntry("Mode", "edgeright");
  else if (m_modeEdgeBottom->isChecked())
     config->writeEntry("Mode", "edgebottom");

  config->writeEntry ("EdgeSize", edgesize);

  config->writeEntry("ShowMenu", m_pShowMenu->isChecked());
  config->writeEntry("ShowMainToolBar", m_pShowMainToolBar->isChecked());
  config->writeEntry("ShowViewToolBar", m_pShowViewToolBar->isChecked());
  config->writeEntry("ShowSettingsToolBar", m_pShowSettingsToolBar->isChecked());

  toolBar("mainToolBar")->saveSettings(config,"Main ToolBar");
  toolBar("viewToolBar")->saveSettings(config,"View ToolBar");
  toolBar("settingsToolBar")->saveSettings(config,"Settings ToolBar");
}


/**
 * Read settings from config file.
 */
void KmagApp::readOptions()
{
  TQColor blue (0,0,128);
  TQColor yellow (255,255,0);
  TQColor white (255,255,255);

  config->setGroup ("WM");
  setTitleColors (
      config->readColorEntry("inactiveBackground", &blue),
      config->readColorEntry("inactiveForeground", &white),
      config->readColorEntry("inactiveTitleBtnBg", &yellow));

  config->setGroup("General Options");

  TQSize defSize(460,390);
  TQSize size=config->readSizeEntry("Geometry", &defSize);
  if(!size.isEmpty())
  {
    resize(size);
  }

  // set zoom - defaults to 2x
  unsigned int zoomIndex = config->readUnsignedNumEntry("ZoomIndex", 4);
  setZoomIndex(zoomIndex);
  emit updateZoomIndex(m_zoomIndex);

  m_pInvert->setChecked (config->readBoolEntry("Invertation", false));
  slotToggleInvert();

  unsigned int rotationIndex = config->readUnsignedNumEntry("RotationIndex", 0);
  setRotationIndex(rotationIndex);
  emit updateRotationIndex(m_rotationIndex);

  unsigned int fpsIndex = config->readUnsignedNumEntry("FPSIndex", 2);
  setFPSIndex(fpsIndex);
  emit updateFPSIndex(m_fpsIndex);

  TQRect defaultRect(0,0,211,164);
  m_zoomView->setSelRectPos(config->readRectEntry("SelRect", &defaultRect));

  m_mouseCursorType = config->readUnsignedNumEntry("ShowMouse", m_defaultMouseCursorType);
  m_zoomView->showMouse(m_mouseCursorType);
  if(m_mouseCursorType)
    m_hideCursor->setChecked(false);
  else
    m_hideCursor->setChecked(true);

  if(config->hasGroup("Settings ToolBar"))
    toolBar("settingsToolBar")->applySettings(config,"Settings ToolBar");

  if(config->hasGroup("Main ToolBar"))
    toolBar("mainToolBar")->applySettings(config,"Main ToolBar");

  if(config->hasGroup("View ToolBar"))
    toolBar("viewToolBar")->applySettings(config,"View ToolBar");

  m_pShowMenu->setChecked(config->readBoolEntry("ShowMenu", true));
  slotShowMenu();

  m_pShowMainToolBar->setChecked(config->readBoolEntry("ShowMainToolBar", false));
  slotShowMainToolBar();

  m_pShowViewToolBar->setChecked(config->readBoolEntry("ShowViewToolBar", true));
  slotShowViewToolBar();

  m_pShowSettingsToolBar->setChecked(config->readBoolEntry("ShowSettingsToolBar", true));
  slotShowSettingsToolBar();

  config->setGroup("General Options");

  edgesize = config->readNumEntry("EdgeSize", 0);
  TQString mode = config->readEntry("Mode", "followmouse");
  if (mode == "selectionwindow")
    slotModeSelWin();
  else if (mode == "edgetop")
    setEdgeMode (m_modeEdgeTop);
  else if (mode == "edgeleft")
    setEdgeMode (m_modeEdgeLeft);
  else if (mode == "edgeright")
    setEdgeMode (m_modeEdgeRight);
  else if (mode == "edgebottom")
    setEdgeMode (m_modeEdgeBottom);
  else
    slotModeFollowMouse();
}

bool KmagApp::queryClose()
{
  return (true);
}

bool KmagApp::queryExit()
{
  saveOptions();
  return true;
}


/////////////////////////////////////////////////////////////////////
// SLOT IMPLEMENTATION
/////////////////////////////////////////////////////////////////////

/**
 * Shows/hides the mouse cursor
 */
void KmagApp::showMouseCursor(bool show)
{
  if(show) {
    if(m_mouseCursorType == 0)
      m_mouseCursorType = m_defaultMouseCursorType;
    m_zoomView->showMouse(m_mouseCursorType);
  } else {
    m_zoomView->showMouse(0);
  }
}

/**
 * Zoom in.
 */
void KmagApp::zoomIn()
{
  // set the new index .. checking will done inside setZoom
  setZoomIndex(m_zoomIndex+1);
  // signal change in zoom index
  emit updateZoomIndex((int)m_zoomIndex);
}

/**
 * Zoom out.
 */
void KmagApp::zoomOut()
{
  // set the new index .. checking will done inside setZoom
  setZoomIndex(m_zoomIndex-1);
  // signal change in zoom index
  emit updateZoomIndex((int)m_zoomIndex);
}
/**
 * Sets the zoom index to index
 */
void KmagApp::setZoomIndex(int index)
{
  if(index < 0 || index >= (int)zoomArray.size()) {
    // the index is invalid
    kdWarning() << "Invalid index!" << endl;
    return;
  } else if((int)m_zoomIndex == index) {
    // do nothing!
    return;
  } else {
    m_zoomIndex = index;
  }

  if(m_zoomIndex == 0) {
    // meaning that no more zooming-out is possible
    // -> disable zoom-out icon
    m_pZoomOut->setEnabled(false);
  } else { // enable the icon
    m_pZoomOut->setEnabled(true);
  }

  if(m_zoomIndex == zoomArray.size()-1) {
    // meaning that no more zooming-in is possible
    // -> disable zoom-in icon
    m_pZoomIn->setEnabled(false);
  } else { // enable the icon
    m_pZoomIn->setEnabled(true);
  }

  // signal change in zoom value
  emit updateZoomValue(zoomArray[m_zoomIndex]);
}

/**
 * Shows/hides the mouse cursor
 */
void KmagApp::slotToggleInvert ()
{
  m_zoomView->setInvertation (m_pInvert->isChecked());
}

/**
 * Sets the rotation index to index
 */
void KmagApp::setRotationIndex(int index)
{
  if(index < 0 || index >= (int)rotationArray.size()) {
    // the index is invalid
    kdWarning() << "Invalid index!" << endl;
    return;
  } else if((int)m_rotationIndex == index) {
    // do nothing!
    return;
  } else {
    m_rotationIndex = index;
  }

  // signal change in zoom value
  emit updateRotationValue(rotationArray[m_rotationIndex]);
}

/**
 * Sets the fps index to index
 */
void KmagApp::setFPSIndex(int index)
{
  if(index < 0 || index >= (int)fpsArray.size()) {
    // the index is invalid
    kdWarning() << "Invalid index!" << endl;
    return;
  } else if((int)m_fpsIndex == index) {
    // do nothing!
    return;
  } else {
    m_fpsIndex = index;
  }

  // signal change in fps value
  emit updateFPSValue(fpsArray[m_fpsIndex]);
}


/**
 * Save the zoomed image
 */
void KmagApp::saveZoomPixmap()
{
  bool toggled(false);

  // stop refresh temporarily
  if (m_zoomView->getRefreshStatus()) {
    slotToggleRefresh();
    toggled = true;
  }

  KURL url = KFileDialog::getSaveURL(TQString(),
              KImageIO::pattern(KImageIO::Writing),
             0,i18n("Save Snapshot As"));

  if(!url.filename().isEmpty()) {
    if(!url.isLocalFile()) {
      // create a temp file.. save image to it.. copy over the n/w and then delete the temp file.
      KTempFile tempFile;
      if(!m_zoomView->getPixmap().save(tempFile.name(), KImageIO::type(url.fileName()).latin1())) {
        KMessageBox::error(0, i18n("Unable to save temporary file (before uploading to the network file you specified)."),
                          i18n("Error Writing File"));
      } else {
        if(!TDEIO::NetAccess::upload(tempFile.name(), url, this)) {
          KMessageBox::error(0, i18n("Unable to upload file over the network."),
                            i18n("Error Writing File"));
        } else {
          KMessageBox::information(0, i18n("Current zoomed image saved to\n%1").arg(url.prettyURL()),
                              i18n("Information"), "save_confirm");
        }
      }
      // remove the temporary file
      tempFile.unlink();

    } else {
      if(!m_zoomView->getPixmap().save(url.path(), KImageIO::type(url.fileName()).latin1())) {
        KMessageBox::error(0, i18n("Unable to save file. Please check if you have permission to write to the directory."),
                            i18n("Error Writing File"));
      } else {
        KMessageBox::information(0, i18n("Current zoomed image saved to\n%1").arg(url.prettyURL()),
                                i18n("Information"), "save_confirm");
      }
    }
  }
  if(toggled) {
    slotToggleRefresh();
  }
}


void KmagApp::slotToggleRefresh()
{
  m_zoomView->toggleRefresh();
  if(m_zoomView->getRefreshStatus()) {
    refreshSwitch->setIcon("stop.png");
    refreshSwitch->setText(i18n("Stop"));
    refreshSwitch->setToolTip(i18n("Click to stop window update"));
  } else {
    refreshSwitch->setIcon("reload.png");
    refreshSwitch->setText(i18n("Start"));
    refreshSwitch->setToolTip(i18n("Click to start window update"));
  }
}


void KmagApp::slotModeSelWin()
{
  unsetEdgeMode(m_modeSelWin);

  m_zoomView->fitToWindow();
  m_zoomView->setFitToWindow (false);
  m_zoomView->followMouse(false);
  m_zoomView->showSelRect(true);
}


void KmagApp::slotModeFollowMouse()
{
  unsetEdgeMode(m_modeFollowMouse);

  m_zoomView->setFitToWindow (true);
  m_zoomView->followMouse(true);
  m_zoomView->showSelRect(false);
}


void KmagApp::slotModeEdgeTop()
{
  // ask for edgesize
  int newedgesize; bool ok;
  newedgesize = KInputDialog::getInteger (i18n ("Magnify to Screen Edge - Select Size"),
                                          i18n ("Size:"), edgesize > 0 ? edgesize : 300, 200,
                                          TQApplication::desktop()->screenGeometry( this ).height()/2,
                                          25, 10, &ok, 0, "getedgesize");

  if (ok) {
    edgesize = newedgesize;
    setEdgeMode(m_modeEdgeTop);
  }
}


void KmagApp::slotModeEdgeLeft()
{
  // ask for edgesize
  int newedgesize; bool ok;
  newedgesize = KInputDialog::getInteger (i18n ("Magnify to Left Screen Edge - Select Size"),
                                          i18n ("Size:"), edgesize > 0 ? edgesize : 300, 200,
                                          TQApplication::desktop()->screenGeometry( this ).width()/2,
                                          25, 10, &ok, 0, "getedgesize");

  if (ok) {
    edgesize = newedgesize;
    setEdgeMode(m_modeEdgeLeft);
  }
}


void KmagApp::slotModeEdgeRight()
{
  // ask for edgesize
  int newedgesize; bool ok;
  newedgesize = KInputDialog::getInteger (i18n ("Magnify to Right Screen Edge - Select Size"),
                                          i18n ("Size:"), edgesize > 0 ? edgesize : 300, 200,
                                          TQApplication::desktop()->screenGeometry( this ).width()/2,
                                          25, 10, &ok, 0, "getedgesize");

  if (ok) {
    edgesize = newedgesize;
    setEdgeMode(m_modeEdgeRight);
  }
}


void KmagApp::slotModeEdgeBottom()
{
  // ask for edgesize
  int newedgesize; bool ok;
  newedgesize = KInputDialog::getInteger (i18n ("Magnify to Bottom Screen Edge - Select Size"),
                                          i18n ("Size:"), edgesize > 0 ? edgesize : 300, 200,
                                          TQApplication::desktop()->screenGeometry( this ).height()/2,
                                          25, 10, &ok, 0, "getedgesize");

  if (ok) {
    edgesize = newedgesize;
    setEdgeMode(m_modeEdgeBottom);
  }
}


void KmagApp::setEdgeMode (TDEToggleAction *mode)
{
  if (m_modeEdgeLeft || mode == m_modeEdgeRight) {
    if (edgesize < 200 || edgesize > TQApplication::desktop()->screenGeometry( this ).width()/2)
      edgesize = TQApplication::desktop()->screenGeometry( this ).width()/4;
  } else {
    if (edgesize < 200 || edgesize > TQApplication::desktop()->screenGeometry( this ).height()/2)
      edgesize = TQApplication::desktop()->screenGeometry( this ).height()/4;
  }

  m_modeFollowMouse->setChecked(false);
  m_modeSelWin->setChecked(false);
  m_modeEdgeTop->setChecked(false);
  m_modeEdgeLeft->setChecked(false);
  m_modeEdgeRight->setChecked(false);
  m_modeEdgeBottom->setChecked(false);
  mode->setChecked (true);

  m_zoomView->setFitToWindow (true);
  m_zoomView->followMouse(true);
  m_zoomView->showSelRect(false);

  m_zoomView->reparent (0, TQPoint(), false);
  KWin::setType(m_zoomView->winId(), NET::Dock);
  KWin::setState(m_zoomView->winId(), NET::Sticky | NET::KeepBelow | NET::SkipTaskbar | NET::SkipPager);
  KWin::setOnAllDesktops(m_zoomView->winId(), true);

  m_pShowMenu->setEnabled (false);
  m_pShowMainToolBar->setEnabled (false);
  m_pShowViewToolBar->setEnabled (false);
  m_pShowSettingsToolBar->setEnabled (false);

  hide();

  if (mode == m_modeEdgeTop) {
    TQRect r = TQApplication::desktop()->screenGeometry( this );
    r.setBottom( r.top() + edgesize );
    m_zoomView->setGeometry ( r );
    KWin::setExtendedStrut (m_zoomView->winId(), 0, 0, 0, 0, 0, 0,
                            edgesize, r.left(), r.right(), 0, 0, 0);
  } else if (mode == m_modeEdgeLeft) {
    TQRect r = TQApplication::desktop()->screenGeometry( this );
    r.setRight( r.left() + edgesize );
    m_zoomView->setGeometry ( r );
    KWin::setExtendedStrut (m_zoomView->winId(),
                            edgesize, r.top(), r.bottom(),
                            0, 0, 0, 0, 0, 0, 0, 0, 0);
  } else if (mode == m_modeEdgeRight) {
    TQRect r = TQApplication::desktop()->screenGeometry( this );
    r.setLeft( r.right() - edgesize );
    m_zoomView->setGeometry ( r );
    KWin::setExtendedStrut (m_zoomView->winId(), 0, 0, 0,
                            edgesize, r.top(), r.bottom(),
                            0, 0, 0, 0, 0, 0);
  } else {
    TQRect r = TQApplication::desktop()->screenGeometry( this );
    r.setTop( r.bottom() - edgesize );
    m_zoomView->setGeometry ( r );
    KWin::setExtendedStrut (m_zoomView->winId(), 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            edgesize, r.left(), r.right());
  }
  m_zoomView->show();
}


void KmagApp::unsetEdgeMode (TDEToggleAction *mode)
{
  edgesize = 0;

  m_modeFollowMouse->setChecked(false);
  m_modeSelWin->setChecked(false);
  m_modeEdgeTop->setChecked(false);
  m_modeEdgeLeft->setChecked(false);
  m_modeEdgeRight->setChecked(false);
  m_modeEdgeBottom->setChecked(false);
  mode->setChecked (true);

  m_zoomView->reparent (this, TQPoint(), true);
  setCentralWidget(m_zoomView);
  KWin::setExtendedStrut (winId(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

  m_pShowMenu->setEnabled (true);
  m_pShowMainToolBar->setEnabled (true);
  m_pShowViewToolBar->setEnabled (true);
  m_pShowSettingsToolBar->setEnabled (true);

  show();
}


void KmagApp::slotToggleHideCursor()
{
  showMouseCursor(!m_hideCursor->isChecked());
}


void KmagApp::slotFileNewWindow()
{
  KmagApp *new_window= new KmagApp();
  new_window->show();
}


void KmagApp::slotFilePrint()
{
#ifndef TQT_NO_PRINTER

  bool toggled(false);

#if TDE_VERSION < 220
  TQPrinter printer;
#else
  KPrinter printer;
#endif

  // stop refresh temporarily
  if (m_zoomView->getRefreshStatus()) {
    slotToggleRefresh();
    toggled = true;
  }

  const TQPixmap pixmap(m_zoomView->getPixmap());

#if TDE_VERSION >= 220
  // use some AI to get the best orientation
  if(pixmap.width() > pixmap.height()) {
    printer.setOrientation(KPrinter::Landscape);
  } else {
    printer.setOrientation(KPrinter::Portrait);
  }
#endif

  if (printer.setup(this)) {
    TQPainter paint;

    if(!paint.begin(&printer))
      return;
    // draw the pixmap
    paint.drawPixmap(0, 0, pixmap);
    // end the painting
    paint.end();
  }

  if(toggled) {
    slotToggleRefresh();
  }
#endif // TQT_NO_PRINTER
}

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
  TDEMainWindow* w;
  if (memberList)
  {
    for(w=memberList->first(); w!=0; w=memberList->first())
    {
      // 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;
      memberList->removeRef(w);
    }
  }
}

void KmagApp::copyToClipBoard()
{
  TQClipboard *cb=TDEApplication::clipboard();
  cb->setPixmap(m_zoomView->getPixmap());
}

void KmagApp::slotShowMenu()
{
  ///////////////////////////////////////////////////////////////////
  // turn Menu on or off
  if(!m_pShowMenu->isChecked())
  {
    menuBar()->hide();
  }
  else
  {
    menuBar()->show();
  }


}

void KmagApp::slotShowMainToolBar()
{
  ///////////////////////////////////////////////////////////////////
  // turn mainToolbar on or off
  if(!m_pShowMainToolBar->isChecked())
  {
    toolBar("mainToolBar")->hide();
  }
  else
  {
    toolBar("mainToolBar")->show();
  }
}

void KmagApp::slotShowViewToolBar()
{
  ///////////////////////////////////////////////////////////////////
  // turn viewToolbar on or off
  if(!m_pShowViewToolBar->isChecked())
  {
    toolBar("viewToolBar")->hide();
  }
  else
  {
    toolBar("viewToolBar")->show();
  }
}

void KmagApp::slotShowSettingsToolBar()
{
  ///////////////////////////////////////////////////////////////////
  // turn viewToolbar on or off
  if(!m_pShowSettingsToolBar->isChecked() || edgesize > 0)
  {
    toolBar("settingsToolBar")->hide();
  }
  else
  {
    toolBar("settingsToolBar")->show();
  }
}

void KmagApp::slotConfKeys()
{
  KKeyDialog::configure( actionCollection() );
}

void KmagApp::slotEditToolbars()
{
  saveMainWindowSettings( TDEGlobal::config(), "MainWindow" );
  KEditToolbar dlg( actionCollection() );
  connect( &dlg, TQ_SIGNAL( newToolbarConfig() ), this, TQ_SLOT( slotNewToolbarConfig() ) );
  if ( dlg.exec() )
    createGUI();
}


void KmagApp::contextMenu (TQPoint pos)
{
 // show popup
 KXMLGUIFactory *factory = this->factory();
 TQPopupMenu *popup = (TQPopupMenu *)factory->container("mainPopUp",this);
 if (popup != 0)
   popup->popup(pos, 0);
}


void KmagApp::slotNewToolbarConfig()
{
  applyMainWindowSettings( TDEGlobal::config(), "MainWindow" );
  createGUI();
}