Set application metadata
[Skavegra.git] / mainwindow.cpp
blob3a1f38d540299bd6c4dcdb554ab0a234c33be702
1 #include "mainwindow.h"
3 MainWindow::MainWindow(QWidget *parent, QString firstFileName) : QMainWindow(parent) {
4 /* Main window properties */
5 setWindowTitle(tr("Skavegra"));
6 resize(400, 400);
7 hasStatusBar = true;
8 hasMenuBar = true;
10 /* Docks */
11 infoDock = new QDockWidget(tr("Meta&data"), this);
12 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
13 infoDock->setFeatures(QDockWidget::DockWidgetClosable);
14 addDockWidget(Qt::RightDockWidgetArea, infoDock);
15 infoDock->hide();
17 /* Menu bar */
18 programMenu = new QMenu(tr("&Program"), this);
19 actionQuit = programMenu->addAction(tr("&Quit"));
20 actionQuit->setIcon(QIcon::fromTheme("application-exit"));
21 actionQuit->setShortcut(Qt::Key_Q);
22 actionQuit->setStatusTip(tr("Close this program"));
23 menuBar()->addMenu(programMenu);
24 toggleMenuBar(hasMenuBar);
26 fileMenu = new QMenu(tr("&File"), this);
27 actionOpen = fileMenu->addAction(tr("&Open"));
28 actionOpen->setIcon(QIcon::fromTheme("document-open"));
29 actionOpen->setStatusTip(tr("Open and display an existing SVG document"));
30 actionOpen->setShortcut(Qt::Key_O);
31 actionReOpen = fileMenu->addAction(tr("&Reload"));
32 actionReOpen->setIcon(QIcon::fromTheme("view-refresh"));
33 actionReOpen->setEnabled(false);
34 actionReOpen->setShortcut(Qt::Key_R);
35 actionReOpen->setStatusTip(tr("Open the currently opened SVG document again to make changes visible"));
36 actionClose = fileMenu->addAction(tr("&Close"));
37 actionClose->setEnabled(false);
38 actionClose->setShortcut(Qt::Key_C);
39 actionClose->setStatusTip(tr("Close the currently opened SVG document"));
40 menuBar()->addMenu(fileMenu);
42 viewMenu = new QMenu(tr("&View"), this);
43 actionZoomIn = viewMenu->addAction(tr("Zoom &in"));
44 actionZoomIn->setIcon(QIcon::fromTheme("zoom-in"));
45 actionZoomIn->setEnabled(false);
46 actionZoomIn->setShortcut(Qt::Key_Plus);
47 actionZoomIn->setStatusTip(tr("Increase the displaying size of the shown image"));
48 actionZoomOut = viewMenu->addAction(tr("Zoom &out"));
49 actionZoomOut->setIcon(QIcon::fromTheme("zoom-out"));
50 actionZoomOut->setEnabled(false);
51 actionZoomOut->setShortcut(Qt::Key_Minus);
52 actionZoomOut->setStatusTip(tr("Decrease the displaying size of the shown image"));
53 actionZoomReset = viewMenu->addAction(tr("&Reset zoom"));
54 actionZoomReset->setIcon(QIcon::fromTheme("zoom-original"));
55 actionZoomReset->setEnabled(false);
56 actionZoomReset->setShortcut(Qt::Key_0);
57 actionZoomReset->setStatusTip(tr("Set the displaying size of the shown image to its default size"));
58 actionZoomBestFit = viewMenu->addAction(tr("&Best fit"));
59 actionZoomBestFit->setIcon(QIcon::fromTheme("zoom-fit-best"));
60 actionZoomBestFit->setEnabled(false);
61 actionZoomBestFit->setShortcut(Qt::Key_F);
62 actionZoomBestFit->setStatusTip(tr("Automatically adjust the displaying size of the shown image to \
63 fill the window, with respect to its aspect ratio"));
64 viewMenu->addSeparator();
65 actionStatusBar = viewMenu->addAction(tr("&Status bar"));
66 actionStatusBar->setCheckable(true);
67 actionStatusBar->setChecked(true);
68 actionStatusBar->setShortcut(Qt::Key_S);
69 actionStatusBar->setStatusTip(tr("Show or hide the status bar"));
70 actionInfoDock = infoDock->toggleViewAction();
71 actionInfoDock->setShortcut(Qt::Key_D);
72 actionInfoDock->setStatusTip(tr("Show or hide additional information about the current image"));
73 viewMenu->addAction(actionInfoDock);
74 menuBar()->addMenu(viewMenu);
76 aboutMenu = new QMenu(tr("&About"), this);
77 actionAbout = aboutMenu->addAction(tr("&About"));
78 actionAbout->setIcon(QIcon::fromTheme("help-about"));
79 actionAbout->setStatusTip(tr("Show information about this program"));
80 menuBar()->addMenu(aboutMenu);
82 /* Status bar */
83 setStatusBar(new QStatusBar());
84 toggleStatusBar(hasStatusBar);
86 /* Central widget: The SVG viewer */
87 svgViewer = new SVGViewer();
88 setCentralWidget(svgViewer);
90 /* Signals and slots */
91 connect(actionQuit, SIGNAL(triggered()), this, SLOT(close()));
92 connect(actionOpen, SIGNAL(triggered()), this, SLOT(openFileDialog()));
93 connect(actionReOpen, SIGNAL(triggered()), this, SLOT(reOpen()));
94 connect(actionClose, SIGNAL(triggered()), this, SLOT(closeFile()));
95 connect(actionZoomIn, SIGNAL(triggered()), svgViewer, SLOT(zoomIn()));
96 connect(actionZoomOut, SIGNAL(triggered()), svgViewer, SLOT(zoomOut()));
97 connect(actionZoomReset, SIGNAL(triggered()), svgViewer, SLOT(zoomReset()));
98 connect(actionZoomBestFit, SIGNAL(triggered()), this, SLOT(zoomBestFit()));
99 connect(actionStatusBar, SIGNAL(toggled(bool)), this, SLOT(toggleStatusBar(bool)));
100 connect(actionAbout, SIGNAL(triggered()), this, SLOT(aboutDialog()));
102 showStatusBarMessage(QString(tr("Skavegra %1 started.")).arg(VERSION_STR), 5000);
104 /* Load first image */
105 imageLoaded = false;
106 if(!firstFileName.isEmpty()) {
107 MainWindow::openFile(firstFileName);
110 updateMetadataText();
113 bool MainWindow::reOpen() {
114 /* Reset the currently opened document and load and render it again */
115 if(lastFileName != NULL) {
116 if(MainWindow::openFile(lastFileName)) {
117 actionClose->setEnabled(true);
118 showStatusBarMessage(QString(tr("File successfully reopened: %1")).arg(lastFileName), 5000);
119 return true;
120 } else {
121 showStatusBarMessage(QString(tr("File could not be reopened: %1")).arg(lastFileName), 5000);
122 return false;
124 } else {
125 return false;
129 void MainWindow::openFileDialog() {
130 /* Open the “Open file …” dialog */
131 QString fileName;
132 fileName = QFileDialog::getOpenFileName(this, tr("Open SVG file …"), ".",
133 tr("Scalable Vector Graphics") + " (*.svg *.svgz *.svg.gz);; " + tr("All files") + "(*)");
134 if (fileName == NULL) {
135 /* No file selected */
136 return;
139 MainWindow::openFile(fileName);
142 bool MainWindow::openFile(QString fileName) {
143 /* Delegate file opening to svgViewer */
144 if(svgViewer->openFile(fileName)) {
145 lastFileName = fileName;
146 showStatusBarMessage(QString(tr("File successfully opened: %1")).arg(fileName), 5000);
147 actionReOpen->setEnabled(true);
148 actionClose->setEnabled(true);
149 actionZoomIn->setEnabled(svgViewer->getZoomAndPan());
150 actionZoomOut->setEnabled(svgViewer->getZoomAndPan());
151 actionZoomReset->setEnabled(svgViewer->getZoomAndPan());
152 actionZoomBestFit->setEnabled(svgViewer->getZoomAndPan());
153 QString title = svgViewer->getTitle();
154 QString dispTitle;
155 if(!title.isNull()) {
156 dispTitle = title;
157 } else {
158 dispTitle = tr("(Untitled)");
160 setWindowTitle(QString(tr("%1—Skavegra")).arg(dispTitle));
161 imageLoaded = true;
162 updateMetadataText();
163 return true;
164 } else {
165 showStatusBarMessage(QString(tr("File could not be opened: %1")).arg(fileName), 5000);
166 return false;
170 void MainWindow::aboutDialog() {
171 QMessageBox::about(this, tr("About Skavegra"),
172 "<h1>"
173 +QString(tr("Skavegra %1")).arg(VERSION_STR)
174 +"</h1><p>"
175 +tr("Skavegra is a simple non-conforming viewer for SVG \
176 (Scalable Vector Graphics) images as specified in the SVG Tiny 1.2 specification.")
177 +"</p><p>"
178 +tr("This software is not finished yet and is unable to display some \
179 SVG images correctly."));
182 void MainWindow::closeFile() {
183 /* Close the current file and reset the view */
184 svgViewer->closeFile();
185 actionReOpen->setEnabled(false);
186 actionClose->setEnabled(false);
187 actionZoomIn->setEnabled(false);
188 actionZoomOut->setEnabled(false);
189 actionZoomReset->setEnabled(false);
190 actionZoomBestFit->setEnabled(false);
191 setWindowTitle(QString(tr("Skavegra")));
192 showStatusBarMessage(QString(tr("File has been closed.")), 5000);
193 imageLoaded = false;
194 updateMetadataText();
197 void MainWindow::toggleMenuBar(bool enabled) {
198 hasMenuBar = enabled;
199 if(!enabled) {
200 menuBar()->hide();
201 } else {
202 menuBar()->show();
206 void MainWindow::toggleStatusBar(bool enabled) {
207 hasStatusBar = enabled;
208 if(!enabled) {
209 statusBar()->hide();
210 } else {
211 statusBar()->show();
215 void MainWindow::showStatusBarMessage(QString string, int timeout) {
216 if(hasStatusBar) {
217 statusBar()->showMessage(string, timeout);
221 void MainWindow::updateMetadataText() {
222 if(imageLoaded) {
223 QString presentedTitle = svgViewer->getTitle();
224 QString presentedDesc = svgViewer->getDesc();
225 QString presentedAnim = boolToYesNo(svgViewer->getAnim());
226 QString presentedZAP;
227 QString properties("");
228 bool propList = false;
229 if(!svgViewer->getZoomAndPan()) {
230 presentedZAP = QString(tr("Zooming and panning is disabled"));
231 propList = true;
232 } else {
233 presentedZAP = QString("");
235 if(svgViewer->getTitle().isNull()) {
236 presentedTitle = "<i>" + tr("Not set") + "</i>";
237 } else {
238 presentedTitle = svgViewer->getTitle();
240 if(svgViewer->getDesc().isNull()) {
241 presentedDesc = "<i>" + tr("Not set") + "</i>";
242 } else {
243 presentedDesc = svgViewer->getDesc();
246 if(propList) {
247 properties = "<strong>" + tr("Special properties") + "</strong><ul><li>" + presentedZAP + "</li></ul>";
250 QLabel* infoText = new QLabel(
251 QString(
252 "<p><strong>"+tr("Title")+"</strong><br>%1</p>"
253 + "<p><strong>"+tr("Description")+"</strong><br>%2</p>"
254 + "<p><strong>"+tr("Animations")+"</strong><br>%3</p>"
255 + properties
256 ).arg(presentedTitle, presentedDesc, presentedAnim));
257 infoText->setWordWrap(true);
258 infoDock->setWidget(infoText);
259 } else {
260 QLabel* infoText = new QLabel("<i>"+tr("No image loaded")+"</i>");
261 infoText->setWordWrap(true);
262 infoDock->setWidget(infoText);
266 void MainWindow::zoomBestFit() {
267 QSizeF imgsize = svgViewer->getImageSize();
268 if(imgsize.isNull()) {
269 return;
271 qreal imgw = (qreal) imgsize.width();
272 qreal imgh = (qreal) imgsize.height();
273 qreal x = -imgw/2;
274 qreal y = -imgh/2;
276 svgViewer->fitInView(QRectF(x, y, imgw, imgh), Qt::KeepAspectRatio);
279 QString MainWindow::boolToYesNo(bool b) {
280 if(b) {
281 return QString(tr("Yes"));
282 } else {
283 return QString(tr("No"));