summaryrefslogtreecommitdiffstats
path: root/src/libgui/toplevel.cpp
blob: c757ace2abc31a7b25444ec12d4278cbcb49ff4b (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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/***************************************************************************
 *   Copyright (C) 2005-2007 Nicolas Hadacek <hadacek@kde.org>             *
 *   Copyright (C) 2003-2004 Alain Gibaud <alain.gibaud@free.fr>           *
 *                                                                         *
 *   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 "toplevel.h"

#include <qpixmap.h>
#include <qiconset.h>
#include <qlayout.h>
#include <qsplitter.h>
#include <qstringlist.h>
#include <qtimer.h>
#include <qprogressbar.h>
#include <qeventloop.h>
#include <qapplication.h>
#include <qtooltip.h>

#include <kstatusbar.h>
#include <klocale.h>
#include <kedittoolbar.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kaction.h>
#include <kcmdlineargs.h>
#include <kio/observer.h>
#include <kiconloader.h>
#include <kpopupmenu.h>
#include <khelpmenu.h>
#include <kmenubar.h>

#include "gui_debug_manager.h"
#include "hex_editor.h"
#include "project_manager.h"
#include "project.h"
#include "global_config.h"
#include "editor.h"
#include "device_gui.h"
#include "text_editor.h"
#include "new_dialogs.h"
#include "common/global/process.h"
#include "common/gui/misc_gui.h"
#include "common/gui/purl_gui.h"
#include "devices/list/device_list.h"
#include "progs/base/prog_config.h"
#include "progs/list/prog_list.h"
#include "progs/gui/prog_group_ui.h"
#include "editor_manager.h"
#include "tools/list/compile_manager.h"
#include "object_view.h"
#include "config_gen.h"
#include "tools/list/compile_config.h"
#include "watch_view.h"
#include "coff/base/text_coff.h"
#include "tools/list/tool_list.h"
#include "breakpoint_view.h"
#include "main_global.h"
#include "gui_prog_manager.h"
#include "devices/gui/register_view.h"
#include "likeback.h"
#include "console.h"
#include "devices/base/device_group.h"
#include "tools/gui/toolchain_config_center.h"
#include "global_config.h"

//----------------------------------------------------------------------------
KDockWidget *MainWindow::createDock(const QString &name, const QPixmap &icon,
                                    const QString &title, const DockPosition &position)
{
  KDockWidget *dock = createDockWidget(name, icon, 0, title);
  dock->setDockSite(KDockWidget::DockCenter);
  DockData ddata;
  ddata.title = title;
  ddata.position = position;
  ddata.dock = dock;
  ddata.action = new ViewMenuAction(dock);
  connect(ddata.action, SIGNAL(activated(QWidget *)), SLOT(toggleToolView(QWidget *)));
  _docks += ddata;
  initDockPosition(ddata);
  return dock;
}

MainWindow::MainWindow()
  : _configGenerator(0), _pikloopsProcess(0), _kfindProcess(0), _forceProgramAfterBuild(false)
{
  Q_ASSERT( Main::_toplevel==0 );
  Main::_toplevel = this;

// status bar
    _actionStatus = new QLabel(statusBar());
    statusBar()->addWidget(_actionStatus);
    _actionProgress = new QProgressBar(statusBar());
    statusBar()->addWidget(_actionProgress);
    _debugStatus = new QLabel(statusBar());
    statusBar()->addWidget(_debugStatus, 0, true);
    _editorStatus = new QLabel(statusBar());
    statusBar()->addWidget(_editorStatus, 0, true);
    _programmerStatus = new ProgrammerStatusWidget(statusBar());
    connect(_programmerStatus, SIGNAL(configure()), SLOT(configureProgrammer()));
    connect(_programmerStatus, SIGNAL(selected(const Programmer::Group &)), SLOT(selectProgrammer(const Programmer::Group &)));
    statusBar()->addWidget(_programmerStatus->widget(), 0, true);
    _toolStatus = new ToolStatusWidget(statusBar());
    connect(_toolStatus, SIGNAL(configureToolchain()), SLOT(configureToolchains()));
    connect(_toolStatus, SIGNAL(configure()), SLOT(configureProject()));
    connect(_toolStatus, SIGNAL(selected(const Tool::Group &)), SLOT(selectTool(const Tool::Group &)));
    statusBar()->addWidget(_toolStatus->widget(), 0, true);

// interface
    _mainDock = createDockWidget("main_dock_widget", QPixmap());
    _mainDock->setDockSite(KDockWidget::DockCorner);
    _mainDock->setEnableDocking(KDockWidget::DockNone);
    setView(_mainDock);
    setMainDockWidget(_mainDock);

    KIconLoader loader;
    KDockWidget *dock = createDock("project_manager_dock_widget", PURL::icon(PURL::Project),
                                   i18n("Project Manager"), DockPosition(KDockWidget::DockLeft, 20));
    Main::_projectManager = new ProjectManager::View(dock);
    connect(Main::_projectManager, SIGNAL(guiChanged()), SLOT(updateGUI()));
    dock->setWidget(Main::_projectManager);

    dock = createDock("watch_view_dock_widget", loader.loadIcon("viewmag", KIcon::Small),
                      i18n("Watch View"), DockPosition("project_manager_dock_widget"));
    Main::_watchView = new Register::WatchView(dock);
    dock->setWidget(Main::_watchView);

    Main::_editorManager = new EditorManager(_mainDock);
    _mainDock->setWidget(Main::_editorManager);
    connect(Main::_editorManager, SIGNAL(guiChanged()), SLOT(updateGUI()));
    connect(Main::_editorManager, SIGNAL(modified(const PURL::Url &)), Main::_projectManager, SLOT(modified(const PURL::Url &)));
    connect(Main::_editorManager, SIGNAL(statusChanged(const QString &)), _editorStatus, SLOT(setText(const QString &)));

    dock = createDock("compile_log_dock_widget", loader.loadIcon("piklab_compile", KIcon::Small),
                      i18n("Compile Log"), DockPosition(KDockWidget::DockBottom, 80));
    Main::_compileLog = new Compile::LogWidget(dock);
    Main::_compileLog->setFocusPolicy(NoFocus);
    dock->setWidget(Main::_compileLog);

    dock = createDock("program_log_dock_widget", loader.loadIcon("piklab_burnchip", KIcon::Small),
                      i18n("Program Log"), DockPosition("compile_log_dock_widget"));
    _programLog = new Log::Widget(dock, "program_log");
    _programLog->setFocusPolicy(NoFocus);
    dock->setWidget(_programLog);

    dock = createDock("breakpoints_dock_widget", loader.loadIcon("piklab_breakpoint_active", KIcon::Small),
                      i18n("Breakpoints"), DockPosition("compile_log_dock_widget"));
    Main::_breakpointsView = new Breakpoint::View(dock);
    Main::_breakpointsView->setFocusPolicy(NoFocus);
    dock->setWidget(Main::_breakpointsView);

    dock = createDock("console_dock_widget", loader.loadIcon("konsole", KIcon::Small),
                      i18n("Konsole"), DockPosition("compile_log_dock_widget"));
    Main::_consoleView = new ConsoleView(dock);
    dock->setWidget(Main::_consoleView);

// managers
    Programmer::manager = new Programmer::GuiManager(this);
    Programmer::manager->setView(_programLog);
    connect(Programmer::manager, SIGNAL(actionMessage(const QString &)), _actionStatus, SLOT(setText(const QString &)));
    connect(Programmer::manager, SIGNAL(showProgress(bool)), SLOT(showProgress(bool)));
    connect(Programmer::manager, SIGNAL(setTotalProgress(uint)), SLOT(setTotalProgress(uint)));
    connect(Programmer::manager, SIGNAL(setProgress(uint)), SLOT(setProgress(uint)));

    Debugger::manager = new Debugger::GuiManager;
    connect(Debugger::manager, SIGNAL(targetStateChanged()), SLOT(updateGUI()));
    connect(Debugger::manager, SIGNAL(statusChanged(const QString &)), _debugStatus, SLOT(setText(const QString &)));
    connect(Debugger::manager, SIGNAL(actionMessage(const QString &)), _actionStatus, SLOT(setText(const QString &)));

    Main::_compileManager = new Compile::Manager(this);
    Main::_compileManager->setView(Main::_compileLog);
    connect(Main::_compileManager, SIGNAL(success()), SLOT(compileSuccess()));
    connect(Main::_compileManager, SIGNAL(failure()), SLOT(compileFailure()));
    connect(Main::_compileManager, SIGNAL(updateFile(const Compile::FileData &)),
            SLOT(updateFile(const Compile::FileData &)));

// actions
    // file actions
    KAction *a = KStdAction::openNew(this, SLOT(newSourceFile()), actionCollection());
    a->setText(i18n("&New Source File..."));
    (void)new KAction(i18n("New hex File..."), "filenew", 0, this, SLOT(newHexFile()),
                      actionCollection(), "file_new_hex");
    KStdAction::open(this, SLOT(openFile()), actionCollection());
    KRecentFilesAction *recent = KStdAction::openRecent(this, SLOT(openRecentFile(const KURL &)), actionCollection());
    recent->setMaxItems(20);
    recent->loadEntries(kapp->config(), "recent-files");
    (void)new KAction(i18n("Save All"), 0, 0, Main::_editorManager, SLOT(saveAllFiles()),
                      actionCollection(), "file_save_all");
    KStdAction::close(Main::_editorManager, SLOT(closeCurrentEditor()), actionCollection());
    (void)new KAction(i18n("C&lose All"), 0, 0, Main::_editorManager, SLOT(closeAllEditors()),
                      actionCollection(), "file_close_all");
    (void)new KAction(i18n("Close All Others"), 0, 0, Main::_editorManager, SLOT(closeAllOtherEditors()),
                      actionCollection(), "file_closeother");
    KStdAction::quit(this, SLOT(close()), actionCollection());

    // edit actions

    // view actions
    (void)new KAction(i18n("Back"), "back", Qt::ALT + Qt::Key_Left,
                      Main::_editorManager, SLOT(goBack()), actionCollection(), "history_back");
    (void)new KAction(i18n("Forward"), "forward", Qt::ALT + Qt::Key_Right,
                      Main::_editorManager, SLOT(goForward()), actionCollection(), "history_forward");
    (void)new KAction(i18n("Switch to..."), 0, Qt::CTRL + Qt::Key_Slash,
                      Main::_editorManager, SLOT(switchToEditor()), actionCollection(), "file_switchto");
    (void)new KAction(i18n("Switch Header/Implementation"), 0, Qt::SHIFT + Qt::Key_F12,
                      Main::_editorManager, SLOT(switchHeaderImplementation()), actionCollection(), "view_switch_source");
    (void)new KAction(QString::null, 0, 0,
                      Debugger::manager, SLOT(toggleBreakpoint()), actionCollection(), "toggle_breakpoint");
    (void)new KAction(QString::null, 0, 0,
                      Debugger::manager, SLOT(toggleEnableBreakpoint()), actionCollection(), "enable_breakpoint");
    (void)new KAction(i18n("Show disassembly location"), 0, 0,
                      Debugger::manager, SLOT(showDisassemblyLocation()), actionCollection(), "show_disassembly_location");
    KActionMenu *toolViewsMenu = new KActionMenu( i18n("Tool Views"), 0, "view_tool_views");
    connect(toolViewsMenu->popupMenu(), SIGNAL(aboutToShow()), SLOT(updateToolViewsActions()));
    actionCollection()->insert(toolViewsMenu);
    a = new KAction(i18n("&Reset Layout"), 0, 0, this, SLOT(resetDockLayout()), actionCollection(), "view_reset_layout");
    toolViewsMenu->insert(a);
    toolViewsMenu->popupMenu()->insertSeparator();
    QValueList<DockData>::iterator it;
    for(it=_docks.begin(); it!=_docks.end(); ++it) toolViewsMenu->insert((*it).action);

    // project actions
    (void)new KAction(i18n("New Project..."), "piklab_createproject", 0,
                      this, SLOT(newProject()), actionCollection(), "project_new");
    (void)new KAction(i18n("Open Project..."), "piklab_openproject", 0,
                      this , SLOT(openProject()), actionCollection(), "project_open");
    recent = new KRecentFilesAction(i18n("Open Recent Project"), 0,
                      this, SLOT(openRecentProject(const KURL &)), actionCollection(), "project_open_recent");
    recent->setMaxItems(20);
    recent->loadEntries(kapp->config(), "recent-projects");
    (void)new KAction(i18n("Project Options..."), "configure", 0,
                      this, SLOT(configureProject()), actionCollection(), "project_options");
    (void)new KAction(i18n("Close Project"), "fileclose", 0,
                      this, SLOT(closeProject()), actionCollection(), "project_close");
    (void)new KAction(i18n("Add Source File..."), "piklab_addfile", 0,
                      Main::_projectManager, SLOT(insertSourceFiles()), actionCollection(), "project_add_source_file");
    (void)new KAction(i18n("Add Object File..."), "piklab_addfile", 0,
                      Main::_projectManager, SLOT(insertObjectFiles()), actionCollection(), "project_add_object_file");
    (void)new KAction(i18n("Add Current File"), "piklab_addcurrentfile", 0,
                      Main::_projectManager, SLOT(insertCurrentFile()), actionCollection(), "project_add_current_file");

    // build actions
    (void)new KAction(i18n("&Build Project"), "piklab_compile", Qt::Key_F8,
                      this, SLOT(buildProject()), actionCollection(), "build_build_project");
    (void)new KAction(i18n("&Compile File"), 0, Qt::SHIFT + Qt::Key_F8,
                      this, SLOT(compileFile()), actionCollection(), "build_compile_file");
    (void)new KAction(i18n("Clean"), "trashcan_empty", 0,
                      this, SLOT(cleanBuild()), actionCollection(), "build_clean");
    (void)new KAction(i18n("Stop"), "stop", 0,
                      this, SLOT(stopBuild()), actionCollection(), "build_stop");

    // programmer actions
    (void)new KAction(i18n("&Connect"), "connect_creating", 0,
                      Programmer::manager, SLOT(connectDevice()), actionCollection(), "prog_connect");
    (void)new KToggleAction(i18n("Device Power"), "piklab_power", 0,
                            Programmer::manager, SLOT(toggleDevicePower()), actionCollection(), "prog_power");
    (void)new KAction(i18n("&Disconnect"), "connect_no", 0,
                      Programmer::manager, SLOT(disconnectDevice()), actionCollection(), "prog_disconnect");
    (void)new KAction(i18n("&Program"), "piklab_burnchip", Qt::SHIFT + Qt::Key_F5,
                      this , SLOT(program()), actionCollection(), "prog_program");
    (void)new KAction(i18n("&Verify"), "piklab_verifychip", Qt::SHIFT + Qt::Key_F6,
                      this , SLOT(verify()), actionCollection(), "prog_verify");
    (void)new KAction(i18n("&Read"), "piklab_readchip", Qt::SHIFT + Qt::Key_F7,
                      this , SLOT(read()), actionCollection(), "prog_read");
    (void)new KAction(i18n("&Erase"), "piklab_erasechip", 0,
                      this, SLOT(erase()), actionCollection(), "prog_erase");
    (void)new KAction(i18n("&Blank Check"), "piklab_blankcheck", 0,
                      this, SLOT(blankCheck()), actionCollection(), "prog_blank_check");
    (void)new KAction(i18n("&Run"), "launch", Qt::SHIFT + Qt::Key_F9,
                      Programmer::manager, SLOT(run()), actionCollection(), "prog_run");
    (void)new KAction(i18n("&Stop"), "piklab_stop", 0,
                      Programmer::manager, SLOT(halt()), actionCollection(), "prog_stop");
    (void)new KAction(i18n("R&estart"), "piklab_restart", 0,
                      Programmer::manager, SLOT(restart()), actionCollection(), "prog_restart");
    (void)new KAction(i18n("&Advanced..."), 0, 0,
                      Programmer::manager , SLOT(showAdvancedDialog()), actionCollection(), "prog_advanced");
    (void)new KAction(i18n("Settings..."), "configure", 0,
                      this , SLOT(showProgrammerSettings()), actionCollection(), "prog_settings");

    // debugger actions
    (void)new KAction(i18n("&Start"), "launch", Qt::SHIFT + Qt::Key_F9,
                      Programmer::manager, SLOT(restart()), actionCollection(), "debug_start");
    (void)new KAction(i18n("&Run"), "piklab_run", Qt::SHIFT + Qt::Key_F9,
                      Programmer::manager, SLOT(run()), actionCollection(), "debug_run");
    (void)new KAction(i18n("&Step"), "piklab_debug_step", 0,
                      Programmer::manager, SLOT(step()), actionCollection(), "debug_next");
    //(void)new KAction(i18n("Step &In"), "piklab_debug_stepin",
    //             0, this, SLOT(debugStepIn()), actionCollection(), "debug_step_in");
    //(void)new KAction(i18n("Step &Out"), "piklab_debug_stepout",
    //             0, this, SLOT(debugStepOut()), actionCollection(), "debug_step_out");
    (void)new KAction(i18n("&Break<Translators: it is the verb>", "&Halt"), "piklab_debughalt", 0,
                      Programmer::manager, SLOT(halt()), actionCollection(), "debug_halt");
    (void)new KAction(i18n("&Disconnect/Stop"), "piklab_stop", 0,
                      Programmer::manager, SLOT(disconnectDevice()), actionCollection(), "debug_stop");
    (void)new KAction(i18n("R&eset"), "piklab_restart", 0,
                      Programmer::manager, SLOT(restart()), actionCollection(), "debug_reset");
    (void)new KAction(i18n("Show Program Counter"), "piklab_program_counter", 0,
                      Debugger::manager, SLOT(showPC()), actionCollection(), "debug_show_pc");
    (void)new KAction(i18n("Clear All Breakpoints"), "remove", 0,
                      Debugger::manager, SLOT(clearBreakpoints()), actionCollection(), "debug_clear_breakpoints");
    (void)new KAction(i18n("Settings..."), "configure", 0,
                      this , SLOT(showDebuggerSettings()), actionCollection(), "debug_settings");

    // tools
    (void)new KAction(i18n("&Pikloops..."), 0, 0,
                      this , SLOT(runPikloops()), actionCollection(), "tools_pikloops");
    (void)new KAction(i18n("&Find Files..."), "find", 0,
                      this , SLOT(runKfind()), actionCollection(), "tools_kfind");
    (void)new KAction(i18n("&Device Information..."), "info", 0,
                      this , SLOT(showDeviceInfo()), actionCollection(), "tools_device_information");
    (void)new KAction(i18n("&Config Generator..."), 0, 0,
                      this , SLOT(configGenerator()), actionCollection(), "tools_config_generator");
    (void)new KAction(i18n("&Template Generator..."), 0, 0,
                      this , SLOT(templateGenerator()), actionCollection(), "tools_template_generator");

    // settings actions
    (void)new KAction(i18n("Configure Toolchains..."), 0, 0,
                      this, SLOT(configureToolchains()), actionCollection(), "options_configure_toolchains");
    (void)KStdAction::preferences(this, SLOT(configure()), actionCollection());

    // help
    (void)new KAction(i18n("Report Bug..."), "likeback_bug", 0,
                      LikeBack::instance(), SLOT(iFoundABug()), actionCollection(), "help_report_bug_piklab");

    setupGUI();
    readDockConfig();

    // LikeBack buttons
    menuBar()->insertItem(new QLabel(menuBar())); // #### first widget is put left-most...
    MenuBarButton *button = new MenuBarButton("likeback_like", menuBar());
    QToolTip::add(button, i18n("I like..."));
    connect(button, SIGNAL(clicked()), LikeBack::instance(), SLOT(iLike()));
    menuBar()->insertItem(button);
    button = new MenuBarButton("likeback_dislike", menuBar());
    QToolTip::add(button, i18n("I do not like..."));
    connect(button, SIGNAL(clicked()), LikeBack::instance(), SLOT(iDoNotLike()));
    menuBar()->insertItem(button);
    button = new MenuBarButton("likeback_bug", menuBar());
    QToolTip::add(button, i18n("I found a bug..."));
    connect(button, SIGNAL(clicked()), LikeBack::instance(), SLOT(iFoundABug()));
    menuBar()->insertItem(button);
    button = new MenuBarButton("configure", menuBar());
    QToolTip::add(button, i18n("Configure email..."));
    connect(button, SIGNAL(clicked()), LikeBack::instance(), SLOT(askEMail()));
    menuBar()->insertItem(button);
    button = new MenuBarButton("help", menuBar());
    connect(button, SIGNAL(clicked()), LikeBack::instance(), SLOT(showWhatsThisMessage()));
    menuBar()->insertItem(button);

    QTimer::singleShot(0, this, SLOT(initialLoading()));
}

MainWindow::~MainWindow()
{}

void MainWindow::readDockConfig()
{
  KDockMainWindow::readDockConfig(kapp->config(), "dock_config");

  // if there is a new dock: it is not displayed by default...
  QMap<QString, QString> entries = kapp->config()->entryMap("dock_config");
  QValueList<DockData>::iterator it;
  for(it=_docks.begin(); it!=_docks.end(); ++it) {
    QMap<QString, QString>::const_iterator eit;
    for(eit=entries.begin(); eit!=entries.end(); ++eit)
      if ( eit.key().startsWith((*it).dock->name()) ) break;
    if ( eit==entries.end() ) initDockPosition(*it);
  }

  // readDockConfig also restore the names/tooltips: what if a new version of the application changes these names...
  for(it=_docks.begin(); it!=_docks.end(); ++it) (*it).dock->setTabPageLabel((*it).title);
  QApplication::postEvent(this, new QEvent(QEvent::CaptionChange));
}

void MainWindow::initialLoading()
{
  ::BusyCursor bc;
  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
  if ( args->count()!=0 ) { // opened urls provided on the command line
    for (int i = 0; i<args->count(); i++) {
      PURL::Url url(args->url(i));
      if ( url.fileType()==PURL::Project ) {
        if ( Main::_projectManager->project()==0 ) Main::_projectManager->openProject(url);
      } else Main::_editorManager->openEditor(url);
    }
  } else { // otherwise reopen last project/files
    Main::_projectManager->openProject(GlobalConfig::openedProject());
    PURL::UrlList files = GlobalConfig::openedFiles();
    PURL::UrlList::const_iterator it = files.begin();
    for (; it!=files.end(); ++it) Main::_editorManager->openEditor(*it);
  }
  updateGUI();
}

void MainWindow::openRecentFile(const KURL &kurl)
{
  Main::_editorManager->openFile(PURL::Url(kurl));
}

void MainWindow::configureToolbar()
{
  saveMainWindowSettings(KGlobal::config(), "MainWindow");
  KEditToolbar dlg(actionCollection());
  connect(&dlg, SIGNAL(newToolbarConfig()), SLOT(applyToolbarSettings()));
  dlg.exec();
}

void MainWindow::applyToolbarSettings()
{
  createGUI();
  applyMainWindowSettings(KGlobal::config(), "MainWindow");
}

void MainWindow::configure(ConfigCenter::Type showType)
{
  stopOperations();
  ConfigCenter dialog(showType, this);
  dialog.exec();
  Programmer::manager->clear();
  updateGUI();
  Debugger::manager->update(true);
}

void MainWindow::configureToolchains()
{
  stopOperations();
  ToolchainsConfigCenter dialog(Main::toolGroup(), this);
  dialog.exec();
  Programmer::manager->clear();
  updateGUI();
  Debugger::manager->update(true);
}

void MainWindow::selectProgrammer(const Programmer::Group &group)
{
  if ( group.name()==Main::programmerGroup().name() ) return;
  bool debugInitialized = Debugger::manager->coff();
  stopOperations();
  GlobalConfig::writeProgrammerGroup(group);
  Programmer::manager->clear();
  updateGUI();
  if (debugInitialized) Debugger::manager->init();
  else Debugger::manager->update(true);
}

void MainWindow::selectTool(const Tool::Group &group)
{
  if ( group.name()==Compile::Config::toolGroup(Main::project()).name() ) return;
  bool debugInitialized = Debugger::manager->coff();
  stopOperations();
  Compile::Config::setToolGroup(Main::project(), group);
  updateGUI();
  if (debugInitialized) Debugger::manager->init();
  else Debugger::manager->update(true);
}

void MainWindow::setDevice(const QString &device)
{
  if ( device==i18n(Device::AUTO_DATA.label) ) Compile::Config::setDevice(Main::project(), Device::AUTO_DATA.name);
  else Compile::Config::setDevice(Main::project(), device);
  updateGUI();
}

void MainWindow::showDeviceInfo()
{
  DeviceChooser::Dialog d(Main::device(), (Main::project() ? DeviceChooser::Choose : DeviceChooser::ChooseWithAuto), this);
  if ( d.exec() ) {
    setDevice(d.device());
    updateGUI();
  }
}

void MainWindow::configGenerator()
{
  PURL::FileType ftype = (Main::currentEditor() ? Main::currentEditor()->fileType() : PURL::Nb_FileTypes);
  PURL::SourceFamily family = (ftype!=PURL::Nb_FileTypes ? ftype.data().sourceFamily : PURL::SourceFamily(PURL::SourceFamily::Nb_Types));
  PURL::ToolType type = (family!=PURL::SourceFamily::Nb_Types ? family.data().toolType : PURL::ToolType(PURL::ToolType::Assembler));
  ConfigGenerator dialog(this);
  dialog.set(Main::deviceData(), Main::toolGroup(), type);
  dialog.exec();
}

void MainWindow::templateGenerator()
{
  PURL::FileType ftype = (Main::currentEditor() ? Main::currentEditor()->fileType() : PURL::Nb_FileTypes);
  PURL::SourceFamily family = (ftype!=PURL::Nb_FileTypes ? ftype.data().sourceFamily : PURL::SourceFamily(PURL::SourceFamily::Nb_Types));
  PURL::ToolType type = (family!=PURL::SourceFamily::Nb_Types ? family.data().toolType : PURL::ToolType(PURL::ToolType::Assembler));
  TemplateGenerator dialog(this);
  dialog.set(Main::deviceData(), Main::toolGroup(), type);
  dialog.exec();
}

bool MainWindow::queryClose()
{
  if ( !stopOperations() ) return false;
  Main::setState(Main::Closing);
  // save list of opened editors
  PURL::UrlList toSave;
  const PURL::UrlList files = Main::_editorManager->files();
  PURL::UrlList::const_iterator it;
  for (it=files.begin(); it!=files.end(); ++it)
    if ( !Main::_projectManager->isExternalFile(*it) ) toSave.append(*it);
  GlobalConfig::writeOpenedFiles(toSave);
  // close editors
  if ( !Main::_editorManager->closeAllEditors() ) {
    Main::setState(Main::Idle);
    return false;
  }
  // save other settings
  ::BusyCursor bc;
  writeDockConfig(kapp->config(), "dock_config");
  static_cast<KRecentFilesAction *>(Main::action("project_open_recent"))->saveEntries(kapp->config(), "recent-projects");
  static_cast<KRecentFilesAction *>(Main::action("file_open_recent"))->saveEntries(kapp->config(), "recent-files");
  GlobalConfig::writeOpenedProject(Main::project() ? Main::project()->url() : PURL::Url());
  if ( Main::project() ) Main::_projectManager->closeProject();
  return true;
}

void MainWindow::newSourceFile()
{
  NewFileDialog dialog(Main::project(), this);
  if ( dialog.exec()!=QDialog::Accepted ) return;
  if ( !dialog.url().exists() && !dialog.url().create(*Main::_compileLog) ) return;
  Main::_editorManager->openEditor(dialog.url());
  if ( dialog.addToProject() ) Main::_projectManager->insertFile(dialog.url());
}

void MainWindow::newHexFile()
{
  if ( Main::device()==Device::AUTO_DATA.name ) {
    MessageBox::sorry(i18n("You need to specify a device to create a new hex file."), Log::Show);
    return;
  }
  QString s;
  for (uint i=0; true; i++) {
    s = i18n("Hex") + (i==0 ? QString::null : QString::number(i));
    if ( Main::_editorManager->findEditor(s)==0 ) break;
  }
  HexEditor *editor = new HexEditor(s, Main::_editorManager);
  editor->memoryRead();
  Main::_editorManager->addEditor(editor);
}

bool MainWindow::openFile()
{
  QString filter;
  filter += PURL::sourceFilter(PURL::SimpleFilter);
  filter += "\n" + PURL::filter(PURL::Hex);
  filter += "\n" + PURL::projectFilter(PURL::SimpleFilter);
  filter += "\n*|" + i18n("All Files");
  PURL::Url url = PURL::getOpenUrl(":open_file", filter, this ,i18n("Open File"));
  if ( url.fileType()==PURL::Project || url.fileType()==PURL::PikdevProject ) {
    stopOperations();
    if ( !Main::_projectManager->openProject(url) ) return false;
    updateGUI();
    return true;
  }
  return Main::_editorManager->openFile(url);
}

void MainWindow::updateGUI()
{
  bool idle = ( Main::_state==Main::Idle );
  switch (Main::_state) {
  case Main::Closing: return;
  case Main::Idle:
    showProgress(false);
    break;
  case Main::Compiling:
    _actionStatus->setText(Main::_compileManager->label());
    showProgress(true);
    makeWidgetDockVisible(Main::_compileLog);
    break;
  case Main::Programming:
    makeWidgetDockVisible(_programLog);
    break;
  }

  // update editor actions
  Main::_editorManager->updateTitles();
  Main::action("file_save_all")->setEnabled(Main::currentEditor());
  Main::action("file_close")->setEnabled(Main::currentEditor());
  Main::action("file_close_all")->setEnabled(Main::currentEditor());
  Main::action("file_closeother")->setEnabled(Main::_editorManager->nbEditors()>1);
  Main::action("options_configure")->setEnabled(idle);
  PURL::FileType currentType = (Main::currentEditor() ? Main::currentEditor()->fileType() : PURL::Nb_FileTypes);
  bool isSource = (currentType==PURL::Nb_FileTypes ? false : currentType.data().group==PURL::Source);
  bool isHeader = (currentType==PURL::Nb_FileTypes ? false : currentType.data().group==PURL::Header);
  Main::action("view_switch_source")->setEnabled(isSource || isHeader);
  Main::action("history_back")->setEnabled(Main::editorManager().history().hasBack());
  Main::action("history_forward")->setEnabled(Main::editorManager().history().hasForward());
  Main::action("show_disassembly_location")->setEnabled(Debugger::manager->coff()!=0 && (isSource || isHeader));

  // update project
  bool inProject = ( Main::currentEditor() && (currentType==PURL::Nb_FileTypes || Main::currentEditor()->url().isEmpty() || Main::_projectManager->contains(Main::currentEditor()->url())) );
  if ( Main::project()==0 && !inProject ) {
    if ( Main::currentEditor()==0 ) Main::_projectManager->closeProject();
    else if ( isSource ) Main::_projectManager->setStandalone(Main::currentEditor()->url(), currentType);
    else {
      PURL::FileType type;
      PURL::Url purl = Main::_projectManager->standaloneGenerator(Main::currentEditor()->url(), type);
      if ( type!=PURL::Nb_FileTypes ) Main::_projectManager->setStandalone(purl, type);
      else if ( currentType==PURL::Hex ) Main::_projectManager->setStandalone(purl, PURL::Hex);
    }
  }
  if ( Main::currentEditor() ) Main::_projectManager->select(Main::currentEditor());

  // update project actions
  Main::action("project_new")->setEnabled(idle);
  Main::action("project_open")->setEnabled(idle);
  Main::action("project_close")->setEnabled(Main::project() && idle);
  Main::action("project_options")->setEnabled(Main::project() && idle);
  Main::action("project_add_source_file")->setEnabled(Main::project() && idle);
  Main::action("project_add_object_file")->setEnabled(Main::project() && idle);
  Main::action("project_add_current_file")->setEnabled(Main::project() && !inProject && idle && isSource);

  // update build actions
  static_cast<PopupButton *>(_toolStatus->widget())->setText(" " + Main::toolGroup().label() + " ");
  bool hexProject = ( Main::_projectManager->projectUrl().fileType()==PURL::Hex );
  bool customTool = Main::toolGroup().isCustom();
  Main::action("build_build_project")->setEnabled((Main::project() || (inProject && !hexProject) ) && idle);
  PURL::Url selected = Main::_projectManager->selectedUrl();
  bool isSelectedSource = ( !selected.isEmpty() && selected.data().group==PURL::Source );
  Main::action("build_compile_file")->setEnabled(!hexProject && (isSource || isSelectedSource) && idle && !customTool);
  Main::action("build_clean")->setEnabled((Main::project() || inProject) && idle && !customTool);
  Main::action("build_stop")->setEnabled(Main::_state==Main::Compiling);

  // update programmer status
  PortType ptype = Programmer::GroupConfig::portType(Main::programmerGroup());
  static_cast<PopupButton *>(_programmerStatus->widget())->setText(" " + Main::programmerGroup().statusLabel(ptype) + " ");
  QFont f = font();
  bool supported = (Main::deviceData() ? Main::programmerGroup().isSupported(Main::deviceData()->name()) : false);
  f.setItalic(!supported);
  _programmerStatus->widget()->setFont(f);
  bool isProgrammer = ( Main::programmerGroup().properties() & ::Programmer::Programmer );
  PURL::Url purl = Main::_projectManager->projectUrl();
  bool hasHex = ( currentType==PURL::Hex || Main::_projectManager->contains(purl.toFileType(PURL::Hex)) );
  Main::action("prog_connect")->setEnabled(isProgrammer && idle);
  Main::action("prog_read")->setEnabled(isProgrammer && idle);
  Main::action("prog_program")->setEnabled(isProgrammer && hasHex && idle);
  Main::action("prog_verify")->setEnabled(isProgrammer && hasHex && idle);
  Main::action("prog_erase")->setEnabled(isProgrammer && idle);
  Main::action("prog_blank_check")->setEnabled(isProgrammer && idle);
  Programmer::State pstate = (Main::programmer() ? Main::programmer()->state() : Programmer::NotConnected);
  static_cast<KToggleAction *>(Main::action("prog_power"))->setEnabled(isProgrammer && idle && pstate!=Programmer::NotConnected);
  Main::action("prog_disconnect")->setEnabled(isProgrammer && idle && pstate!=Programmer::NotConnected);
  bool isDebugger = ( Main::programmerGroup().properties() & ::Programmer::Debugger );
  bool resetAvailable = ( Main::programmerGroup().properties() & Programmer::CanReleaseReset );
  Main::action("prog_run")->setEnabled(idle && resetAvailable && !isDebugger && pstate!=Programmer::Running);
  Main::action("prog_stop")->setEnabled(idle && !isDebugger && pstate==Programmer::Running);
  Main::action("prog_restart")->setEnabled(idle && !isDebugger && pstate==Programmer::Running);
  const Programmer::GroupUI *pgui = static_cast<const Programmer::GroupUI *>(Main::programmerGroup().gui());
  Main::action("prog_advanced")->setEnabled(idle && pgui->hasAdvancedDialog());

  // update debugger status
  Debugger::manager->updateDevice();
  Main::action("debug_start")->setEnabled(idle && isDebugger && pstate!=Programmer::Running && pstate!=Programmer::Halted);
  Main::action("debug_run")->setEnabled(idle && isDebugger && pstate!=Programmer::Running && !Debugger::manager->isStepping() );
  Main::action("debug_halt")->setEnabled(idle && isDebugger && (pstate==Programmer::Running || Debugger::manager->isStepping()) );
  Main::action("debug_stop")->setEnabled(idle && isDebugger && (pstate==Programmer::Running || pstate==Programmer::Halted));
  Main::action("debug_next")->setEnabled(idle && isDebugger && pstate!=Programmer::Running);
  Main::action("debug_reset")->setEnabled(idle && isDebugger && (pstate==Programmer::Running || pstate==Programmer::Halted));
  Main::action("debug_show_pc")->setEnabled(idle && isDebugger && Debugger::manager->coff()!=0 && Debugger::manager->pc().isInitialized() );

  Main::_projectManager->updateGUI();

  // caption
  QString caption;
  if ( Main::project() ) caption += Main::project()->name();
  if ( Main::currentEditor() ) {
    if ( Main::project() ) caption += " - ";
    caption += Main::currentEditor()->url().filepath();
  }
  setCaption(KApplication::kApplication()->makeStdCaption(caption));

  emit stateChanged();
}

void MainWindow::updateToolViewsActions()
{
  QValueList<DockData>::iterator it;
  for(it=_docks.begin(); it!=_docks.end(); ++it) (*it).action->setChecked((*it).dock->mayBeHide());
}

void MainWindow::initDockPosition(const DockData &ddata)
{
  const DockPosition &pos = ddata.position;
  ddata.dock->manualDock(manager()->getDockWidgetFromName(pos.parent), pos.pos, pos.space);
}

void MainWindow::resetDockLayout()
{
  QValueList<DockData>::iterator it;
  for (it=_docks.begin(); it!=_docks.end(); ++it) initDockPosition(*it);
}

void MainWindow::toggleToolView(QWidget *widget)
{
  static_cast<KDockWidget *>(widget)->changeHideShowState();
}

void MainWindow::runKfind()
{
  if (_kfindProcess) return;
  _kfindProcess = new ::Process::StringOutput;
  QString path;
  PURL::Url url = Main::projectManager().projectUrl();
  if ( !url.isEmpty() ) path = url.path();
  _kfindProcess->setup("kfind", path, false);
  connect(_kfindProcess, SIGNAL(done(int)), SLOT(kfindDone()));
  if ( !_kfindProcess->start(0) )
    MessageBox::sorry(i18n("Could not run \"kfind\""), Log::Show);
}

void MainWindow::kfindDone()
{
  delete _kfindProcess;
  _kfindProcess = 0;
}

void MainWindow::runPikloops()
{
  if (_pikloopsProcess) return;
  _pikloopsProcess = new ::Process::StringOutput;
  _pikloopsProcess->setup("pikloops", QStringList(), false);
  connect(_pikloopsProcess, SIGNAL(done(int)), SLOT(pikloopsDone()));
  if ( !_pikloopsProcess->start(0) )
    MessageBox::detailedSorry(i18n("Could not run \"pikloops\""), i18n("The Pikloops utility (%1) is not installed in your system.").arg("http://pikloops.sourceforge.net"), Log::Show);
}

void MainWindow::pikloopsDone()
{
  delete _pikloopsProcess;
  _pikloopsProcess = 0;
}

//-----------------------------------------------------------------------------
void MainWindow::compileFile()
{
  Editor *e = Main::currentEditor();
  if ( e && e->isModified() ) e->save(); // buffer is systematically saved
  stopOperations();
  Main::_compileLog->clear();
  PURL::Url url = (e ? e->url() : Main::_projectManager->selectedUrl());
  bool generated = (e ? Main::_projectManager->isExternalFile(url) : false);
  if ( Main::project()==0 || !generated ) Main::_projectManager->removeExternalFiles();
  url = (!generated ? url : Main::_projectManager->projectUrl());
  if ( Main::_compileManager->compileFile(Compile::TodoItem(url, generated)) ) Main::setState(Main::Compiling);
  else compileFailure();
}

void MainWindow::buildProject()
{
  if ( Main::project()==0 ) {
    compileFile();
    return;
  }
  // save modified buffers
  PURL::UrlList files = Main::project()->absoluteFiles();
  PURL::UrlList::const_iterator it;
  for (it=files.begin(); it!=files.end(); ++it) {
    // save modified editors
    Editor *e = Main::_editorManager->findEditor(*it);
    if ( e && e->isModified() ) e->save();
  }
  bool tmp = _forceProgramAfterBuild;
  stopOperations();
  _forceProgramAfterBuild = tmp;
  Main::_compileLog->clear();
  Main::_projectManager->removeExternalFiles();
  Compile::LinkType ltype = (Main::programmerGroup().name()=="icd2_debugger" ? Compile::Icd2Linking : Compile::NormalLinking);
  if ( Main::_compileManager->buildProject(ltype) ) Main::setState(Main::Compiling);
  else compileFailure();
}

void MainWindow::compileFailure()
{
  _forceProgramAfterBuild = false;
  Main::setState(Main::Idle);
}

void MainWindow::compileSuccess()
{
  if ( !Main::_compileManager->compileOnly() ) {
    Main::_projectManager->setModified(false);
    if ( Main::project()->outputType()==Tool::OutputType::Executable ) {
      if ( Debugger::manager->init() ) {
        const QStringList &included = Debugger::manager->coff()->filenames();
        QStringList::const_iterator it;
        for (it=included.begin(); it!=included.end(); ++it) {
          PURL::Directory dir = (Main::project() ? Main::project()->directory() : Main::projectManager().projectUrl().directory());
          PURL::Url url = PURL::Url::fromPathOrUrl(*it).toAbsolute(dir);
          if ( !url.exists() ) continue;
          Main::_projectManager->addExternalFile(url, ProjectManager::Included);
        }
      }
      if ( _forceProgramAfterBuild || readConfigEntry(BaseGlobalConfig::ProgramAfterBuild).toBool() ) program();
    }
  }
  _forceProgramAfterBuild = false;
  Main::setState(Main::Idle);
}

void MainWindow::updateFile(const Compile::FileData &fdata)
{
  if ( fdata.actions & Compile::InProject ) {
    if ( fdata.actions & Compile::Generated ) Main::_projectManager->addExternalFile(fdata.url, ProjectManager::Generated);
    else if ( fdata.actions & Compile::Included ) Main::_projectManager->addExternalFile(fdata.url, ProjectManager::Included);
    else Q_ASSERT(false);
  }
  if ( fdata.actions & Compile::Show ) {
    Editor *e = Main::_editorManager->openEditor(fdata.url);
    if (e) e->setReadOnly(true);
  }
}

void MainWindow::cleanBuild()
{
  stopOperations();
  Main::_compileLog->clear();
  if ( Main::project() ) {
    Compile::LinkType ltype = (Main::programmerGroup().name()=="icd2_debugger" ? Compile::Icd2Linking : Compile::NormalLinking);
    Main::_compileManager->cleanProject(ltype);
  } else {
    PURL::FileType type;
    PURL::Url url = Main::_projectManager->standaloneGenerator(Main::currentEditor()->url(), type);
    Main::_compileManager->cleanFile(url);
  }
  Main::_projectManager->removeExternalFiles();
}

void MainWindow::stopBuild()
{
  Main::_compileManager->kill();
}

//-----------------------------------------------------------------------------
void MainWindow::keyPressEvent(QKeyEvent *e)
{
  if ( e->key()==Key_Escape ) stopOperations();
}

bool MainWindow::stopOperations()
{
  if ( Main::_state==Main::Programming ) {
    _programLog->log(Log::LineType::Warning, i18n("Programming in progress. Cannot be aborted."));
    return false;
  }
  stopBuild();
  Programmer::manager->stop();
  Debugger::manager->clear();
  return true;
}

void MainWindow::newProject()
{
  stopOperations();
  Main::_projectManager->newProject();
  updateGUI();
  Main::_compileLog->clear();
}

void MainWindow::openProject()
{
  stopOperations();
  Main::_projectManager->openProject();
  updateGUI();
  Main::_compileLog->clear();
}

void MainWindow::openRecentProject(const KURL &url)
{
  stopOperations();
  Main::_projectManager->openProject(PURL::Url(url));
  updateGUI();
  Main::_compileLog->clear();
}

void MainWindow::configureProject()
{
  stopOperations();
  if ( Main::project()==0 ) configure(ConfigCenter::Standalone);
  else Main::_projectManager->editProject();
  updateGUI();
}

void MainWindow::closeProject()
{
  stopOperations();
  Main::_projectManager->closeProject();
  updateGUI();
  Main::_compileLog->clear();
}

//----------------------------------------------------------------------------
HexEditor *MainWindow::getHexEditor()
{
  if ( Main::_projectManager->isModified() || Main::_projectManager->needsRecompile() ) {
    MessageBox::Result res = MessageBox::Yes;
    if ( !readConfigEntry(BaseGlobalConfig::AutoRebuildModified).toBool() ) {
      res = MessageBox::questionYesNoCancel(i18n("The project hex file may not be up-to-date since some project files have been modified."),
                                            i18n("Recompile First"), i18n("Continue Anyway"));
      if ( res==MessageBox::Cancel ) return 0;
    }
    if ( res==MessageBox::Yes ) {
      _forceProgramAfterBuild = true;
      buildProject();
      return 0;
    }
  }
  if ( Main::currentEditor() && Main::currentEditor()->fileType()==PURL::Hex )
    return static_cast<HexEditor *>(Main::currentEditor());
  PURL::Url purl = Main::_projectManager->projectUrl();
  HexEditor *editor = static_cast<HexEditor *>(Main::_editorManager->openEditor(purl.toFileType(PURL::Hex)));
  if ( editor==0 ) return 0;
  editor->setReadOnly(true);
  return editor;
}

void MainWindow::erase()
{
  Programmer::manager->erase(Device::MemoryRange());
}

void MainWindow::blankCheck()
{
  Programmer::manager->blankCheck(Device::MemoryRange());
}

void MainWindow::program()
{
  HexEditor *editor = getHexEditor();
  if ( editor==0 ) return;
  if ( Main::programmerGroup().isDebugger() && !Main::_projectManager->contains(editor->url()) ) {
    MessageBox::sorry(i18n("It is not possible to start a debugging session with an hex file not generated with the current project."), Log::Show);
    return;
  }
  Programmer::manager->program(*editor->memory(), Device::MemoryRange());
}

void MainWindow::verify()
{
  HexEditor *editor = getHexEditor();
  if ( editor==0 ) return;
  Programmer::manager->verify(*editor->memory(), Device::MemoryRange());
}

void MainWindow::read()
{
  QString s = i18n("Read");
  Editor *e = Main::_editorManager->findEditor(s);
  if (e) Main::_editorManager->closeEditor(e, true);
  HexEditor *editor = new HexEditor(s, Main::_editorManager);
  editor->setDevice();
  if ( Programmer::manager->read(*editor->memory(), Device::MemoryRange()) ) {
    editor->memoryRead();
    Main::_editorManager->addEditor(editor);
  } else delete editor;
}

void MainWindow::showProgress(bool show)
{
  if (show) {
    BusyCursor::start();
    _actionStatus->show();
    _actionProgress->show();
  } else {
    BusyCursor::stop();
    _actionStatus->hide();
    _actionProgress->hide();
  }
}

void MainWindow::setTotalProgress(uint nb)
{
  //KIO::Job *job = new KIO::SimpleJob(KURL(), 0, 0, false);
  //int id = Observer::self()->newJob(job, true);
  //Observer::self()->slotTotalSize(job, total);
  //Observer::self()->slotInfoMessage(job, "test");
  //qDebug("set total steps: %i", total);
  _actionProgress->setTotalSteps(nb);
  _actionProgress->setProgress(0);
  QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput); // #### DANGER !!!!
}

void MainWindow::setProgress(uint nb)
{
  _actionProgress->setProgress(nb);
  QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput); // #### DANGER !!!!
}