Several GUI fixes
[pierogi] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 #include <QtCore/QCoreApplication>
5 #include <QMutex>
6 #include <QtGui/QMessageBox>
7 #include <QSettings>
8 #include <QMaemo5InformationBox>
9
10 #include "pirkeysetwidgetitem.h"
11 #include "pirselectkeysetform.h"
12 #include "pirsecondaryform.h"
13 #include "pirdocumentationform.h"
14 #include "piraboutform.h"
15 #include "pirkeysetmanager.h"
16
17 //#define DEBUGGING
18
19 // Some ugly globals used for thread communications:
20
21 // A global to show that a command is being processed:
22 bool commandInFlight = false;
23 QMutex commandIFMutex;
24
25 // The stopRepeatingFlag boolean is the method used to tell running commands
26 // in the worker thread to stop:
27 bool stopRepeatingFlag = false;
28 QMutex stopRepeatingMutex;
29
30 extern PIRMakeMgr makeManager;
31
32
33 MainWindow::MainWindow(QWidget *parent)
34   : QMainWindow(parent),
35     ui(new Ui::MainWindow),
36     selectKeysetForm(0),
37     secondaryForm(0),
38     documentationForm(0),
39     aboutForm(0),
40     currentKeyset(0)
41 {
42   ui->setupUi(this);
43
44   // Make this a Maemo 5 stacked widget:
45   setAttribute(Qt::WA_Maemo5StackedWindow);
46
47   // Collect the keysets:
48   myKeysets = new PIRKeysetManager();
49
50   // Set up the keyset selection window:
51   selectKeysetForm = new PIRSelectKeysetForm(this);
52
53   // Set up the secondary buttons window:
54   secondaryForm = new PIRSecondaryForm(this);
55
56   myKeysets->populateGuiWidget(selectKeysetForm);
57
58   // Remember any favorites the user has already set:
59   populateFavorites();
60
61   QSettings settings("pietrzak.org", "Pierogi");
62   if (settings.contains("currentKeysetName"))
63   {
64     myKeysets->findKeysetID(
65       settings.value("currentKeysetMake").toString(),
66       settings.value("currentKeysetName").toString(),
67       currentKeyset);
68 //    currentKeyset = settings.value("currentKeyset").toInt();
69   }
70
71   enableButtons();
72
73   connect(
74     ui->favoriteKeysetsWidget,
75     SIGNAL(itemActivated(QListWidgetItem *)),
76     this,
77     SLOT(keysetSelectionChanged(QListWidgetItem *)),
78     Qt::QueuedConnection);
79
80   // Make sure the two selection lists don't show different selections:
81   QListWidget *klw = selectKeysetForm->getKeysetListWidget();
82   connect(
83     ui->favoriteKeysetsWidget,
84     SIGNAL(itemActivated(QListWidgetItem *)),
85     klw,
86     SLOT(clearSelection()),
87     Qt::QueuedConnection);
88
89   connect(
90     klw,
91     SIGNAL(itemActivated(QListWidgetItem *)),
92     ui->favoriteKeysetsWidget,
93     SLOT(clearSelection()),
94     Qt::QueuedConnection);
95
96 #ifndef DEBUGGING
97   // The PIRModprobe object should take care of setting up and shutting down
98   // the lirc_rx51 kernel module, if necessary:
99  
100   if (modprobeObj.loadRX51Module() != 0)
101   {
102     // Couldn't load module, quit:
103     QMessageBox errBox;
104     errBox.setText("Couldn't load lirc_rx51 kernel module!");
105     errBox.setIcon(QMessageBox::Warning);
106     errBox.exec();
107 //    throw; // Need a clean way to exit here!!!
108   }
109 #endif
110 }
111
112
113 MainWindow::~MainWindow()
114 {
115   delete myKeysets;
116   if (selectKeysetForm) delete selectKeysetForm;
117   if (documentationForm) delete documentationForm;
118   if (aboutForm) delete aboutForm;
119   delete ui;
120 }
121
122
123 void MainWindow::setOrientation(ScreenOrientation orientation)
124 {
125 #if defined(Q_OS_SYMBIAN)
126     // If the version of Qt on the device is < 4.7.2, that attribute won't work
127     if (orientation != ScreenOrientationAuto) {
128         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
129         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
130             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
131             return;
132         }
133     }
134 #endif // Q_OS_SYMBIAN
135
136     Qt::WidgetAttribute attribute;
137     switch (orientation) {
138 #if QT_VERSION < 0x040702
139     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
140     case ScreenOrientationLockPortrait:
141         attribute = static_cast<Qt::WidgetAttribute>(128);
142         break;
143     case ScreenOrientationLockLandscape:
144         attribute = static_cast<Qt::WidgetAttribute>(129);
145         break;
146     default:
147     case ScreenOrientationAuto:
148         attribute = static_cast<Qt::WidgetAttribute>(130);
149         break;
150 #else // QT_VERSION < 0x040702
151     case ScreenOrientationLockPortrait:
152         attribute = Qt::WA_LockPortraitOrientation;
153         break;
154     case ScreenOrientationLockLandscape:
155         attribute = Qt::WA_LockLandscapeOrientation;
156         break;
157     default:
158     case ScreenOrientationAuto:
159         attribute = Qt::WA_AutoOrientation;
160         break;
161 #endif // QT_VERSION < 0x040702
162     };
163     setAttribute(attribute, true);
164 }
165
166 void MainWindow::showExpanded()
167 {
168 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
169     showFullScreen();
170 #elif defined(Q_WS_MAEMO_5)
171     showMaximized();
172 #else
173     show();
174 #endif
175 }
176
177
178 void MainWindow::enableButtons()
179 {
180   // Just to be sure, check to see if the keyset has been populated:
181   myKeysets->populateKeyset(this, currentKeyset);
182
183   // This is going to be a little painful...
184   // Main keys
185   emit powerEnabled(myKeysets->hasKey(currentKeyset, Power_Key));
186   emit volumeUpEnabled(myKeysets->hasKey(currentKeyset, VolumeUp_Key));
187   emit volumeDownEnabled(myKeysets->hasKey(currentKeyset, VolumeDown_Key));
188   emit channelUpEnabled(myKeysets->hasKey(currentKeyset, ChannelUp_Key));
189   emit channelDownEnabled(myKeysets->hasKey(currentKeyset, ChannelDown_Key));
190   emit muteEnabled(myKeysets->hasKey(currentKeyset, Mute_Key));
191
192   // Main tab labels:
193   emit keysetMakeChanged(
194     makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
195   emit keysetNameChanged(myKeysets->getDisplayName(currentKeyset));
196
197   // Utility keys:
198   emit redEnabled(myKeysets->hasKey(currentKeyset, Red_Key));
199   emit greenEnabled(myKeysets->hasKey(currentKeyset, Green_Key));
200   emit yellowEnabled(myKeysets->hasKey(currentKeyset, Yellow_Key));
201   emit blueEnabled(myKeysets->hasKey(currentKeyset, Blue_Key));
202   emit pictureModeEnabled(myKeysets->hasKey(currentKeyset, PictureMode_Key));
203   emit soundModeEnabled(myKeysets->hasKey(currentKeyset, SoundMode_Key));
204   emit aspectRatioEnabled(myKeysets->hasKey(currentKeyset, AspectRatio_Key));
205 //  emit surroundEnabled(myKeysets->hasKey(currentKeyset, Surround_Key));
206   emit audioEnabled(myKeysets->hasKey(currentKeyset, Audio_Key));
207   emit infoEnabled(myKeysets->hasKey(currentKeyset, Info_Key));
208   emit captionsEnabled(myKeysets->hasKey(currentKeyset, Captions_Key));
209   emit sleepEnabled(myKeysets->hasKey(currentKeyset, Sleep_Key));
210   emit inputEnabled(myKeysets->hasKey(currentKeyset, Input_Key));
211
212   // Keypad keys
213   emit zeroEnabled(myKeysets->hasKey(currentKeyset, Zero_Key));
214   emit oneEnabled(myKeysets->hasKey(currentKeyset, One_Key));
215   emit twoEnabled(myKeysets->hasKey(currentKeyset, Two_Key));
216   emit threeEnabled(myKeysets->hasKey(currentKeyset, Three_Key));
217   emit fourEnabled(myKeysets->hasKey(currentKeyset, Four_Key));
218   emit fiveEnabled(myKeysets->hasKey(currentKeyset, Five_Key));
219   emit sixEnabled(myKeysets->hasKey(currentKeyset, Six_Key));
220   emit sevenEnabled(myKeysets->hasKey(currentKeyset, Seven_Key));
221   emit eightEnabled(myKeysets->hasKey(currentKeyset, Eight_Key));
222   emit nineEnabled(myKeysets->hasKey(currentKeyset, Nine_Key));
223   emit enterEnabled(myKeysets->hasKey(currentKeyset, Enter_Key));
224   emit clearEnabled(myKeysets->hasKey(currentKeyset, Clear_Key));
225   emit dashEnabled(myKeysets->hasKey(currentKeyset, Dash_Key));
226   emit plusOneHundredEnabled(myKeysets->hasKey(currentKeyset, PlusOneHundred_Key));
227   emit doubleDigitEnabled(myKeysets->hasKey(currentKeyset, DoubleDigit_Key));
228   emit prevChannelEnabled(myKeysets->hasKey(currentKeyset, PrevChannel_Key));
229
230   // Menu keys:
231   emit upEnabled(myKeysets->hasKey(currentKeyset, Up_Key));
232   emit downEnabled(myKeysets->hasKey(currentKeyset, Down_Key));
233   emit leftEnabled(myKeysets->hasKey(currentKeyset, Left_Key));
234   emit rightEnabled(myKeysets->hasKey(currentKeyset, Right_Key));
235   emit selectEnabled(myKeysets->hasKey(currentKeyset, Select_Key));
236   emit menuEnabled(myKeysets->hasKey(currentKeyset, Menu_Key));
237   emit exitEnabled(myKeysets->hasKey(currentKeyset, Exit_Key));
238   emit guideEnabled(myKeysets->hasKey(currentKeyset, Guide_Key));
239   emit discMenuEnabled(myKeysets->hasKey(currentKeyset, DiscMenu_Key));
240
241   // Media keys:
242   emit nextEnabled(myKeysets->hasKey(currentKeyset, Next_Key));
243   emit previousEnabled(myKeysets->hasKey(currentKeyset, Previous_Key));
244   emit advanceEnabled(myKeysets->hasKey(currentKeyset, Advance_Key));
245   emit replayEnabled(myKeysets->hasKey(currentKeyset, Replay_Key));
246   emit stepForwardEnabled(myKeysets->hasKey(currentKeyset, StepForward_Key));
247   emit stepBackEnabled(myKeysets->hasKey(currentKeyset, StepBack_Key));
248   emit fastForwardEnabled(myKeysets->hasKey(currentKeyset, FastForward_Key));
249   emit reverseEnabled(myKeysets->hasKey(currentKeyset, Rewind_Key));
250   emit playEnabled(myKeysets->hasKey(currentKeyset, Play_Key));
251   emit pauseEnabled(myKeysets->hasKey(currentKeyset, Pause_Key));
252   emit stopEnabled(myKeysets->hasKey(currentKeyset, Stop_Key));
253   emit ejectEnabled(myKeysets->hasKey(currentKeyset, Eject_Key));
254
255   // Also enable the buttons on the secondary form:
256   secondaryForm->enableButtons(myKeysets, currentKeyset);
257 }
258
259
260 void MainWindow::receivedExternalWarning(
261   const char *warning)
262 {
263   QMessageBox errBox;
264   errBox.setText(warning);
265   errBox.setIcon(QMessageBox::Warning);
266   errBox.exec();
267 }
268
269
270 // Main tab buttons:
271
272 void MainWindow::on_powerButton_pressed()
273 {
274   startRepeating(Power_Key);
275 }
276
277 void MainWindow::on_powerButton_released()
278 {
279   stopRepeating();
280 }
281
282 void MainWindow::on_mainChannelUpButton_pressed()
283 {
284   startRepeating(ChannelUp_Key);
285 }
286
287 void MainWindow::on_mainChannelUpButton_released()
288 {
289   stopRepeating();
290 }
291
292 void MainWindow::on_mainChannelDownButton_pressed()
293 {
294   startRepeating(ChannelDown_Key);
295 }
296
297 void MainWindow::on_mainChannelDownButton_released()
298 {
299   stopRepeating();
300 }
301
302 void MainWindow::on_mainVolumeUp_pressed()
303 {
304   startRepeating(VolumeUp_Key);
305 }
306
307 void MainWindow::on_mainVolumeUp_released()
308 {
309   stopRepeating();
310 }
311
312 void MainWindow::on_mainVolumeDownButton_pressed()
313 {
314   startRepeating(VolumeDown_Key);
315 }
316
317 void MainWindow::on_mainVolumeDownButton_released()
318 {
319   stopRepeating();
320 }
321
322 void MainWindow::on_muteButton_pressed()
323 {
324   startRepeating(Mute_Key);
325 }
326
327 void MainWindow::on_muteButton_released()
328 {
329   stopRepeating();
330 }
331
332
333 // Utility tab buttons:
334
335 void MainWindow::on_redButton_pressed()
336 {
337   startRepeating(Red_Key);
338 }
339
340 void MainWindow::on_redButton_released()
341 {
342   stopRepeating();
343 }
344
345 void MainWindow::on_greenButton_pressed()
346 {
347   startRepeating(Green_Key);
348 }
349
350 void MainWindow::on_greenButton_released()
351 {
352   stopRepeating();
353 }
354
355 void MainWindow::on_yellowButton_pressed()
356 {
357   startRepeating(Yellow_Key);
358 }
359
360 void MainWindow::on_yellowButton_released()
361 {
362   stopRepeating();
363 }
364
365 void MainWindow::on_blueButton_pressed()
366 {
367   startRepeating(Blue_Key);
368 }
369
370 void MainWindow::on_blueButton_released()
371 {
372   stopRepeating();
373 }
374
375 void MainWindow::on_pictureModeButton_pressed()
376 {
377   startRepeating(PictureMode_Key);
378 }
379
380 void MainWindow::on_pictureModeButton_released()
381 {
382   stopRepeating();
383 }
384
385 void MainWindow::on_soundModeButton_pressed()
386 {
387   startRepeating(SoundMode_Key);
388 }
389
390 void MainWindow::on_soundModeButton_released()
391 {
392   stopRepeating();
393 }
394
395 void MainWindow::on_aspectRatioButton_pressed()
396 {
397   startRepeating(AspectRatio_Key);
398 }
399
400 void MainWindow::on_aspectRatioButton_released()
401 {
402   stopRepeating();
403 }
404
405 /*
406 void MainWindow::on_surroundButton_pressed()
407 {
408   startRepeating(Surround_Key);
409 }
410
411 void MainWindow::on_surroundButton_released()
412 {
413   stopRepeating();
414 }
415 */
416
417 void MainWindow::on_audioButton_pressed()
418 {
419   startRepeating(Audio_Key);
420 }
421
422 void MainWindow::on_audioButton_released()
423 {
424   stopRepeating();
425 }
426
427 void MainWindow::on_infoButton_pressed()
428 {
429   startRepeating(Info_Key);
430 }
431
432 void MainWindow::on_infoButton_released()
433 {
434   stopRepeating();
435 }
436
437 void MainWindow::on_captionButton_pressed()
438 {
439   startRepeating(Captions_Key);
440 }
441
442 void MainWindow::on_captionButton_released()
443 {
444   stopRepeating();
445 }
446
447 void MainWindow::on_inputButton_pressed()
448 {
449   startRepeating(Input_Key);
450 }
451
452 void MainWindow::on_inputButton_released()
453 {
454   stopRepeating();
455 }
456
457 void MainWindow::on_sleepButton_pressed()
458 {
459   startRepeating(Sleep_Key);
460 }
461
462 void MainWindow::on_sleepButton_released()
463 {
464   stopRepeating();
465 }
466
467
468 // Keypad tab buttons:
469
470 void MainWindow::on_oneButton_pressed()
471 {
472   startRepeating(One_Key);
473 }
474
475 void MainWindow::on_oneButton_released()
476 {
477   stopRepeating();
478 }
479
480 void MainWindow::on_twoButton_pressed()
481 {
482   startRepeating(Two_Key);
483 }
484
485 void MainWindow::on_twoButton_released()
486 {
487   stopRepeating();
488 }
489
490 void MainWindow::on_threeButton_pressed()
491 {
492   startRepeating(Three_Key);
493 }
494
495 void MainWindow::on_threeButton_released()
496 {
497   stopRepeating();
498 }
499
500 void MainWindow::on_fourButton_pressed()
501 {
502   startRepeating(Four_Key);
503 }
504
505 void MainWindow::on_fourButton_released()
506 {
507   stopRepeating();
508 }
509
510 void MainWindow::on_fiveButton_pressed()
511 {
512   startRepeating(Five_Key);
513 }
514
515 void MainWindow::on_fiveButton_released()
516 {
517   stopRepeating();
518 }
519
520 void MainWindow::on_sixButton_pressed()
521 {
522   startRepeating(Six_Key);
523 }
524
525 void MainWindow::on_sixButton_released()
526 {
527   stopRepeating();
528 }
529
530 void MainWindow::on_sevenButton_pressed()
531 {
532   startRepeating(Seven_Key);
533 }
534
535 void MainWindow::on_sevenButton_released()
536 {
537   stopRepeating();
538 }
539
540 void MainWindow::on_eightButton_pressed()
541 {
542   startRepeating(Eight_Key);
543 }
544
545 void MainWindow::on_eightButton_released()
546 {
547   stopRepeating();
548 }
549
550 void MainWindow::on_nineButton_pressed()
551 {
552   startRepeating(Nine_Key);
553 }
554
555 void MainWindow::on_nineButton_released()
556 {
557   stopRepeating();
558 }
559
560 void MainWindow::on_zeroButton_pressed()
561 {
562   startRepeating(Zero_Key);
563 }
564
565 void MainWindow::on_zeroButton_released()
566 {
567   stopRepeating();
568 }
569
570 void MainWindow::on_enterButton_pressed()
571 {
572   startRepeating(Enter_Key);
573 }
574
575 void MainWindow::on_enterButton_released()
576 {
577   stopRepeating();
578 }
579
580 void MainWindow::on_clearButton_pressed()
581 {
582   startRepeating(Clear_Key);
583 }
584
585 void MainWindow::on_clearButton_released()
586 {
587   stopRepeating();
588 }
589
590 void MainWindow::on_prevChannelButton_pressed()
591 {
592   startRepeating(PrevChannel_Key);
593 }
594
595 void MainWindow::on_prevChannelButton_released()
596 {
597   stopRepeating();
598 }
599
600 void MainWindow::on_plusOneHundredButton_pressed()
601 {
602   startRepeating(PlusOneHundred_Key);
603 }
604
605 void MainWindow::on_plusOneHundredButton_released()
606 {
607   stopRepeating();
608 }
609
610 void MainWindow::on_dashButton_pressed()
611 {
612   startRepeating(Dash_Key);
613 }
614
615 void MainWindow::on_dashButton_released()
616 {
617   stopRepeating();
618 }
619
620 void MainWindow::on_doubleDigitButton_pressed()
621 {
622   startRepeating(DoubleDigit_Key);
623 }
624
625 void MainWindow::on_doubleDigitButton_released()
626 {
627   stopRepeating();
628 }
629
630
631 // Menu tab buttons:
632
633 void MainWindow::on_upButton_pressed()
634 {
635   startRepeating(Up_Key);
636 }
637
638 void MainWindow::on_upButton_released()
639 {
640   stopRepeating();
641 }
642
643 void MainWindow::on_leftButton_pressed()
644 {
645   startRepeating(Left_Key);
646 }
647
648 void MainWindow::on_leftButton_released()
649 {
650   stopRepeating();
651 }
652
653 void MainWindow::on_rightButton_pressed()
654 {
655   startRepeating(Right_Key);
656 }
657
658 void MainWindow::on_rightButton_released()
659 {
660   stopRepeating();
661 }
662
663 void MainWindow::on_downButton_pressed()
664 {
665   startRepeating(Down_Key);
666 }
667
668 void MainWindow::on_downButton_released()
669 {
670   stopRepeating();
671 }
672
673 void MainWindow::on_selectButton_pressed()
674 {
675   startRepeating(Select_Key);
676 }
677
678 void MainWindow::on_selectButton_released()
679 {
680   stopRepeating();
681 }
682
683 void MainWindow::on_menuButton_pressed()
684 {
685   startRepeating(Menu_Key);
686 }
687
688 void MainWindow::on_menuButton_released()
689 {
690   stopRepeating();
691 }
692
693 void MainWindow::on_exitButton_pressed()
694 {
695   startRepeating(Exit_Key);
696 }
697
698 void MainWindow::on_exitButton_released()
699 {
700   stopRepeating();
701 }
702
703 void MainWindow::on_guideButton_pressed()
704 {
705   startRepeating(Guide_Key);
706 }
707
708 void MainWindow::on_guideButton_released()
709 {
710   stopRepeating();
711 }
712
713 void MainWindow::on_discMenuButton_pressed()
714 {
715   startRepeating(DiscMenu_Key);
716 }
717
718 void MainWindow::on_discMenuButton_released()
719 {
720   stopRepeating();
721 }
722
723
724 // Media tab buttons:
725
726 void MainWindow::on_mediaPreviousButton_pressed()
727 {
728   startRepeating(Previous_Key);
729 }
730
731 void MainWindow::on_mediaPreviousButton_released()
732 {
733   stopRepeating();
734 }
735
736 void MainWindow::on_mediaNextButton_pressed()
737 {
738   startRepeating(Next_Key);
739 }
740
741 void MainWindow::on_mediaNextButton_released()
742 {
743   stopRepeating();
744 }
745
746 void MainWindow::on_replayButton_pressed()
747 {
748   startRepeating(Replay_Key);
749 }
750
751 void MainWindow::on_replayButton_released()
752 {
753   stopRepeating();
754 }
755
756 void MainWindow::on_advanceButton_pressed()
757 {
758   startRepeating(Advance_Key);
759 }
760
761 void MainWindow::on_advanceButton_released()
762 {
763   stopRepeating();
764 }
765
766 void MainWindow::on_stepBackButton_pressed()
767 {
768   startRepeating(StepBack_Key);
769 }
770
771 void MainWindow::on_stepBackButton_released()
772 {
773   stopRepeating();
774 }
775
776 void MainWindow::on_stepForwardButton_pressed()
777 {
778   startRepeating(StepForward_Key);
779 }
780
781 void MainWindow::on_stepForwardButton_released()
782 {
783   stopRepeating();
784 }
785
786 void MainWindow::on_reverseButton_pressed()
787 {
788   startRepeating(Rewind_Key);
789 }
790
791 void MainWindow::on_reverseButton_released()
792 {
793   stopRepeating();
794 }
795
796 void MainWindow::on_fastForwardButton_pressed()
797 {
798   startRepeating(FastForward_Key);
799 }
800
801 void MainWindow::on_fastForwardButton_released()
802 {
803   stopRepeating();
804 }
805
806 void MainWindow::on_playButton_pressed()
807 {
808   startRepeating(Play_Key);
809 }
810
811 void MainWindow::on_playButton_released()
812 {
813   stopRepeating();
814 }
815
816 void MainWindow::on_pauseButton_pressed()
817 {
818   startRepeating(Pause_Key);
819 }
820
821 void MainWindow::on_pauseButton_released()
822 {
823   stopRepeating();
824 }
825
826 void MainWindow::on_stopButton_pressed()
827 {
828   startRepeating(Stop_Key);
829 }
830
831 void MainWindow::on_stopButton_released()
832 {
833   stopRepeating();
834 }
835
836 void MainWindow::on_ejectButton_pressed()
837 {
838   startRepeating(Eject_Key);
839 }
840
841 void MainWindow::on_ejectButton_released()
842 {
843   stopRepeating();
844 }
845
846
847 // Menu actions:
848
849 void MainWindow::on_actionSelectKeyset_triggered()
850 {
851   selectKeysetForm->show();
852 }
853
854 void MainWindow::on_actionSecondary_Buttons_triggered()
855 {
856   if (!secondaryForm)
857   {
858     secondaryForm = new PIRSecondaryForm(this);
859   }
860
861   secondaryForm->show();
862 }
863
864 void MainWindow::on_actionAbout_triggered()
865 {
866   if (!aboutForm)
867   {
868     aboutForm = new PIRAboutForm(this);
869   }
870
871   aboutForm->show();
872 }
873
874 void MainWindow::on_actionDocumentation_triggered()
875 {
876   if (!documentationForm)
877   {
878     documentationForm = new PIRDocumentationForm(this);
879   }
880
881   documentationForm->show();
882 }
883
884
885 // Other actions:
886
887 void MainWindow::keysetSelectionChanged(
888   QListWidgetItem *item)
889 {
890   if (!item) return;  // Should probably display error message here!
891
892   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
893
894   if (!kwi) return; // Also need to say something here
895
896   if (currentKeyset == kwi->getID())
897   {
898     // We're already on that keyset, so nothing to do:
899     return;
900   }
901   
902   currentKeyset = kwi->getID();
903
904   QSettings settings("pietrzak.org", "Pierogi");
905 //  settings.setValue("currentKeyset", currentKeyset);
906
907   settings.setValue(
908     "currentKeysetMake",
909     makeManager.getMakeString(kwi->getMake()));
910
911   settings.setValue(
912     "currentKeysetName",
913     myKeysets->getDisplayName(currentKeyset));
914
915   enableButtons();
916 }
917
918
919 void MainWindow::finalCleanup()
920 {
921   // Perform any necessary cleanup work here.
922
923   // Make certain that the thread stops repeating:
924   stopRepeating();
925 }
926
927
928 void MainWindow::on_addKeysetButton_clicked()
929 {
930   // Is the current keyset already a favorite?
931   int count = ui->favoriteKeysetsWidget->count();
932   int index = 0;
933   PIRKeysetWidgetItem *kwi = NULL;
934   while (index < count)
935   {
936     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
937       ui->favoriteKeysetsWidget->item(index));
938
939     if (kwi && (kwi->getID() == currentKeyset))
940     {
941       // Current keyset already in list!  No need to continue.
942       return;
943     }
944     ++index;
945   }
946
947   // Ok, add the current keyset to the favorites:
948   PIRMakeName make = myKeysets->getMake(currentKeyset);
949
950   QString name = makeManager.getMakeString(make);
951   name.append(" ");
952   name.append(myKeysets->getDisplayName(currentKeyset));
953
954   ui->favoriteKeysetsWidget->addItem(
955     new PIRKeysetWidgetItem(name, currentKeyset, make));
956
957   // And, add the keyset id to the persistent list:
958   QSettings settings("pietrzak.org", "Pierogi");
959
960   int favSettingsSize = settings.beginReadArray("favorites");
961   settings.endArray();
962
963   settings.beginWriteArray("favorites");
964   settings.setArrayIndex(favSettingsSize);
965 //  settings.setValue("keysetID", currentKeyset);
966
967   settings.setValue(
968     "keysetMake",
969     makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
970
971   settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
972
973   settings.endArray();
974 }
975
976
977 void MainWindow::on_removeKeysetButton_clicked()
978 {
979   // Deleting an item removes it from the list, so just grab the currently
980   // selected item and delete it:
981   QListWidgetItem *item = ui->favoriteKeysetsWidget->currentItem();
982
983   if (item) delete item;
984
985   // Remove this item from the persistent list.  Well, actually, it seems a
986   // little more convenient to just blow away the existing list of favorites
987   // and rewrite it, as modifying an existing QSettings array in the middle
988   // seems a bit hard...
989   QSettings settings("pietrzak.org", "Pierogi");
990
991   settings.remove("favorites");
992
993   int count = ui->favoriteKeysetsWidget->count();
994
995   // If the count is empty, we can stop right here:
996   if (count == 0) return;
997
998   int index = 0;
999   unsigned int id;
1000   PIRKeysetWidgetItem *kwi = NULL;
1001   settings.beginWriteArray("favorites");
1002   while (index < count)
1003   {
1004     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
1005       ui->favoriteKeysetsWidget->item(index));
1006
1007     settings.setArrayIndex(index);
1008 //    settings.setValue("keysetID", kwi->getID());
1009     id = kwi->getID();
1010
1011     settings.setValue(
1012       "keysetMake",
1013       makeManager.getMakeString(myKeysets->getMake(id)));
1014
1015     settings.setValue("keysetName", myKeysets->getDisplayName(id));
1016
1017     ++index;
1018   }
1019   settings.endArray();
1020 }
1021
1022 void MainWindow::populateFavorites()
1023 {
1024   QSettings settings("pietrzak.org", "Pierogi");
1025
1026   int size = settings.beginReadArray("favorites");
1027   int index = 0;
1028   QString make;
1029   QString name;
1030   PIRKeysetWidgetItem *kwi;
1031
1032   while (index < size)
1033   {
1034     settings.setArrayIndex(index);
1035     make = settings.value("keysetMake").toString();
1036     name = settings.value("keysetName").toString();
1037
1038     kwi = myKeysets->makeKeysetItem(make, name);
1039
1040     // Did the item creation work?
1041     if (kwi)
1042     {
1043       // Keyset does exist, so continue:
1044       ui->favoriteKeysetsWidget->addItem(kwi);
1045     }
1046
1047     ++index;
1048   }
1049
1050   settings.endArray();
1051 }
1052
1053
1054 void MainWindow::startRepeating(
1055   PIRKeyName name)
1056 {
1057   QMutexLocker locker(&commandIFMutex);
1058   if (!commandInFlight)
1059   {
1060     commandInFlight = true;
1061     emit buttonPressed(currentKeyset, name);
1062   }
1063 }
1064
1065
1066 void MainWindow::stopRepeating()
1067 {
1068   QMutexLocker locker(&stopRepeatingMutex);
1069   stopRepeatingFlag = true;
1070 }
1071
1072
1073 QWidget *MainWindow::getSecondaryWindow()
1074 {
1075   return secondaryForm;
1076 }
1077
1078
1079 void MainWindow::selectPrevFavKeyset()
1080 {
1081   int size = ui->favoriteKeysetsWidget->count();
1082
1083   if (size == 0)
1084   {
1085     // No favorites, so nothing to do!
1086     return;
1087   }
1088
1089   int position = ui->favoriteKeysetsWidget->currentRow();
1090
1091   --position;
1092   if (position < 0)
1093   {
1094     position = size - 1;
1095   }
1096
1097   ui->favoriteKeysetsWidget->setCurrentRow(
1098     position,
1099     QItemSelectionModel::ClearAndSelect);
1100
1101   keysetSelectionChanged(ui->favoriteKeysetsWidget->currentItem());
1102
1103   // Tell the user about the change:
1104   QMaemo5InformationBox::information(
1105     0,
1106     ui->favoriteKeysetsWidget->item(position)->text());
1107 }
1108
1109
1110 void MainWindow::selectNextFavKeyset()
1111 {
1112   int size = ui->favoriteKeysetsWidget->count();
1113
1114   if (size == 0)
1115   {
1116     // No favorites, so just return:
1117     return;
1118   }
1119
1120   int position = ui->favoriteKeysetsWidget->currentRow();
1121
1122   ++position;
1123   if (position == size)
1124   {
1125     position = 0;
1126   }
1127
1128   ui->favoriteKeysetsWidget->setCurrentRow(
1129     position,
1130     QItemSelectionModel::ClearAndSelect);
1131
1132   keysetSelectionChanged(ui->favoriteKeysetsWidget->currentItem());
1133
1134   // Tell the user about the change:
1135   QMaemo5InformationBox::information(
1136     0,
1137     ui->favoriteKeysetsWidget->item(position)->text());
1138 }