|
1 #include <QFileDialog> |
|
2 #include <QMessageBox> |
|
3 |
|
4 #include "Document.h" |
|
5 #include "mainwindow.h" |
|
6 #include "ui_mainwindow.h" |
|
7 |
|
8 MainWindow::MainWindow(QWidget *parent) |
|
9 : QMainWindow(parent), ui(new Ui::MainWindow) |
|
10 { |
|
11 ui->setupUi(this); |
|
12 // MDI from Chap 4, page 105 Foundamentals of Qt Development |
|
13 workspace = new QWorkspace(); |
|
14 setCentralWidget(workspace); |
|
15 connect(workspace, SIGNAL(windowActivated(QWidget*)), |
|
16 this, SLOT(enableActions())); |
|
17 mapper = new QSignalMapper(this); |
|
18 connect(mapper, SIGNAL(mapped(QWidget*)), |
|
19 workspace, SLOT(setActiveWindow(QWidget*))); |
|
20 |
|
21 this->createActions(); |
|
22 this->enableActions(); |
|
23 } |
|
24 |
|
25 MainWindow::~MainWindow() |
|
26 { |
|
27 delete ui; |
|
28 } |
|
29 |
|
30 void MainWindow::notYetImplemented() |
|
31 { |
|
32 QMessageBox::information(this, tr("Build Log Viewer"), tr("Not Yet Implemented.")); |
|
33 } |
|
34 |
|
35 void MainWindow::openLog() |
|
36 { |
|
37 QString filename = QFileDialog::getOpenFileName( |
|
38 this, tr("Open Log"), QDir::currentPath(), |
|
39 tr("Build log *.xml;;All files (*.*)")); |
|
40 Document *doc = new Document(this, filename); |
|
41 workspace->addWindow(doc); |
|
42 |
|
43 doc->show(); |
|
44 } |
|
45 |
|
46 void MainWindow::createActions() |
|
47 { |
|
48 // file menu - mark as NYI |
|
49 connect(ui->actionOpen, SIGNAL(triggered()), |
|
50 this, SLOT(openLog())); |
|
51 connect(ui->actionClose, SIGNAL(triggered()), |
|
52 workspace, SLOT(closeActiveWindow())); |
|
53 |
|
54 // find menu - mark as NYI |
|
55 connect(ui->actionFind, SIGNAL(triggered()), |
|
56 this, SLOT(notYetImplemented())); |
|
57 connect(ui->actionTags , SIGNAL(triggered()), |
|
58 this, SLOT(notYetImplemented())); |
|
59 |
|
60 // window menu - mark as NYI |
|
61 connect(ui->actionTile , SIGNAL(triggered()), |
|
62 this, SLOT(notYetImplemented())); |
|
63 connect(ui->actionCascade , SIGNAL(triggered()), |
|
64 this, SLOT(notYetImplemented())); |
|
65 connect(ui->actionNext_Window , SIGNAL(triggered()), |
|
66 this, SLOT(notYetImplemented())); |
|
67 connect(ui->actionPrevious_Window , SIGNAL(triggered()), |
|
68 this, SLOT(notYetImplemented())); |
|
69 |
|
70 ui->actionSeparator->setSeparator(true); |
|
71 } |
|
72 |
|
73 void MainWindow::enableActions() |
|
74 { |
|
75 bool hasDocuments = (activeDocument() != 0) ; |
|
76 // file |
|
77 ui->actionOpen->setEnabled(true); |
|
78 ui->actionClose->setEnabled(hasDocuments); |
|
79 ui->actionQuit->setEnabled(true); |
|
80 // find |
|
81 ui->actionFind->setEnabled(hasDocuments); |
|
82 ui->actionTags->setEnabled(hasDocuments); |
|
83 // window |
|
84 ui->actionCascade->setEnabled(hasDocuments); |
|
85 ui->actionTile->setEnabled(hasDocuments); |
|
86 ui->actionNext_Window->setEnabled(hasDocuments); |
|
87 ui->actionPrevious_Window->setEnabled(hasDocuments); |
|
88 ui->actionSeparator->setEnabled(hasDocuments); |
|
89 } |
|
90 |
|
91 void MainWindow::updateWindowList() |
|
92 { |
|
93 ui->menuWindow->clear(); |
|
94 |
|
95 ui->menuWindow->addAction(ui->actionTile); |
|
96 ui->menuWindow->addAction(ui->actionCascade); |
|
97 ui->menuWindow->addSeparator(); |
|
98 ui->menuWindow->addAction(ui->actionNext_Window); |
|
99 ui->menuWindow->addAction(ui->actionPrevious_Window); |
|
100 // If there are no open logs, we will disable this one. |
|
101 ui->menuWindow->addAction(ui->actionSeparator); |
|
102 |
|
103 int i=1; |
|
104 foreach(QWidget *w, workspace->windowList()) |
|
105 { |
|
106 QString text; |
|
107 if ( i< 10) { |
|
108 text = QString("&%1 %2").arg(i++).arg(w->windowTitle()); |
|
109 } else { |
|
110 text = w->windowTitle(); |
|
111 } |
|
112 |
|
113 QAction *action = ui->menuWindow->addAction(text); |
|
114 action->setCheckable(true); |
|
115 action->setChecked(w == activeDocument()); |
|
116 connect(action,SIGNAL(triggered()), |
|
117 mapper, SLOT(map())); |
|
118 mapper->setMapping(action, w); |
|
119 } |
|
120 } |
|
121 |
|
122 void MainWindow::closeEvent(QCloseEvent *event) |
|
123 { |
|
124 workspace->closeAllWindows(); |
|
125 } |
|
126 |
|
127 Document * MainWindow::activeDocument() |
|
128 { |
|
129 return qobject_cast<Document*>(workspace->activeWindow()); |
|
130 } |