Qt 5.10 Book Examples
MDIProgram.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // MDIProgram.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include "MDIProgram.h"
13 #include "DocWindow.h"
14 
15 // ----------------------------------------------------------------------
16 MDIProgram::MDIProgram(QWidget* pwgt/*=0*/) : QMainWindow(pwgt)
17 {
18  QAction* pactNew = new QAction("New File", 0);
19  pactNew->setText("&New");
20  pactNew->setShortcut(QKeySequence("CTRL+N"));
21  pactNew->setToolTip("New Document");
22  pactNew->setStatusTip("Create a new file");
23  pactNew->setWhatsThis("Create a new file");
24  pactNew->setIcon(QPixmap(":/filenew.png"));
25  connect(pactNew, SIGNAL(triggered()), SLOT(slotNewDoc()));
26 
27  QAction* pactOpen = new QAction("Open File", 0);
28  pactOpen->setText("&Open...");
29  pactOpen->setShortcut(QKeySequence("CTRL+O"));
30  pactOpen->setToolTip("Open Document");
31  pactOpen->setStatusTip("Open an existing file");
32  pactOpen->setWhatsThis("Open an existing file");
33  pactOpen->setIcon(QPixmap(":/fileopen.png"));
34  connect(pactOpen, SIGNAL(triggered()), SLOT(slotLoad()));
35 
36  QAction* pactSave = new QAction("Save File", 0);
37  pactSave->setText("&Save");
38  pactSave->setShortcut(QKeySequence("CTRL+S"));
39  pactSave->setToolTip("Save Document");
40  pactSave->setStatusTip("Save the file to disk");
41  pactSave->setWhatsThis("Save the file to disk");
42  pactSave->setIcon(QPixmap(":/filesave.png"));
43  connect(pactSave, SIGNAL(triggered()), SLOT(slotSave()));
44 
45  QToolBar* ptbFile = new QToolBar("File Operations");
46  ptbFile->addAction(pactNew);
47  ptbFile->addAction(pactOpen);
48  ptbFile->addAction(pactSave);
49  addToolBar(Qt::TopToolBarArea, ptbFile);
50 
51  QMenu* pmnuFile = new QMenu("&File");
52  pmnuFile->addAction(pactNew);
53  pmnuFile->addAction(pactOpen);
54  pmnuFile->addAction(pactSave);
55  pmnuFile->addAction("Save &As...", this, SLOT(slotSaveAs()));
56  pmnuFile->addSeparator();
57  pmnuFile->addAction("&Quit",
58  qApp,
59  SLOT(closeAllWindows()),
60  QKeySequence("CTRL+Q")
61  );
62  menuBar()->addMenu(pmnuFile);
63 
64  m_pmnuWindows = new QMenu("&Windows");
65  menuBar()->addMenu(m_pmnuWindows);
66  connect(m_pmnuWindows, SIGNAL(aboutToShow()), SLOT(slotWindows()));
67  menuBar()->addSeparator();
68 
69  QMenu* pmnuHelp = new QMenu("&Help");
70  pmnuHelp->addAction("&About", this, SLOT(slotAbout()), Qt::Key_F1);
71  menuBar()->addMenu(pmnuHelp);
72 
73  m_pma = new QMdiArea;
74  m_pma->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
75  m_pma->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
76  setCentralWidget(m_pma);
77 
78  m_psigMapper = new QSignalMapper(this);
79  connect(m_psigMapper,
80  SIGNAL(mapped(QWidget*)),
81  this,
82  SLOT(slotSetActiveSubWindow(QWidget*))
83  );
84 
85  statusBar()->showMessage("Ready", 3000);
86 }
87 
88 // ----------------------------------------------------------------------
89 void MDIProgram::slotNewDoc()
90 {
91  createNewDoc()->show();
92 }
93 
94 // ----------------------------------------------------------------------
95 DocWindow* MDIProgram::createNewDoc()
96 {
97  DocWindow* pdoc = new DocWindow;
98  m_pma->addSubWindow(pdoc);
99  pdoc->setAttribute(Qt::WA_DeleteOnClose);
100  pdoc->setWindowTitle("Unnamed Document");
101  pdoc->setWindowIcon(QPixmap(":/filenew.png"));
102  connect(pdoc,
103  SIGNAL(changeWindowTitle(const QString&)),
104  SLOT(slotChangeWindowTitle(const QString&))
105  );
106 
107  return pdoc;
108 }
109 
110 // ----------------------------------------------------------------------
111 void MDIProgram::slotChangeWindowTitle(const QString& str)
112 {
113  qobject_cast<DocWindow*>(sender())->setWindowTitle(str);
114 }
115 
116 // ----------------------------------------------------------------------
117 void MDIProgram::slotLoad()
118 {
119  DocWindow* pdoc = createNewDoc();
120  pdoc->slotLoad();
121  pdoc->show();
122 }
123 
124 // ----------------------------------------------------------------------
125 void MDIProgram::slotSave()
126 {
127  DocWindow* pdoc = qobject_cast<DocWindow*>(m_pma->activeSubWindow());
128  if (pdoc) {
129  pdoc->slotSave();
130  }
131 }
132 
133 // ----------------------------------------------------------------------
134 void MDIProgram::slotSaveAs()
135 {
136  DocWindow* pdoc = qobject_cast<DocWindow*>(m_pma->activeSubWindow());
137  if (pdoc) {
138  pdoc->slotSaveAs();
139  }
140 }
141 
142 // ----------------------------------------------------------------------
143 void MDIProgram::slotAbout()
144 {
145  QMessageBox::about(this, "Application", "MDI Example");
146 }
147 
148 // ----------------------------------------------------------------------
149 void MDIProgram::slotWindows()
150 {
151  m_pmnuWindows->clear();
152 
153  QAction* pact = m_pmnuWindows->addAction("&Cascade",
154  m_pma,
155  SLOT(cascadeSubWindows())
156  );
157  pact->setEnabled(!m_pma->subWindowList().isEmpty());
158 
159  pact = m_pmnuWindows->addAction("&Tile",
160  m_pma,
161  SLOT(tileSubWindows())
162  );
163  pact->setEnabled(!m_pma->subWindowList().isEmpty());
164 
165  m_pmnuWindows->addSeparator();
166 
167  QList<QMdiSubWindow*> listDoc = m_pma->subWindowList();
168  for (int i = 0; i < listDoc.size(); ++i) {
169  pact = m_pmnuWindows->addAction(listDoc.at(i)->windowTitle());
170  pact->setCheckable(true);
171  pact->setChecked(m_pma->activeSubWindow() == listDoc.at(i));
172  connect(pact, SIGNAL(triggered()), m_psigMapper, SLOT(map()));
173  m_psigMapper->setMapping(pact, listDoc.at(i));
174  }
175 }
176 
177 // ----------------------------------------------------------------------
178 void MDIProgram::slotSetActiveSubWindow(QWidget* pwgt)
179 {
180  if (pwgt) {
181  m_pma->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(pwgt));
182  }
183 }
void slotLoad()
Definition: DocWindow.cpp:20
void slotSaveAs()
Definition: DocWindow.cpp:55
void slotSave()
Definition: DocWindow.cpp:39
MDIProgram(QWidget *pwgt=0)
Definition: MDIProgram.cpp:16