|
| 1 | +/*****************************************************************//** |
| 2 | + * \file mainwindow.cpp |
| 3 | + * \brief |
| 4 | + * |
| 5 | + * \author Xuhua Huang |
| 6 | + * \date November 2022 |
| 7 | + *********************************************************************/ |
| 8 | + |
| 9 | +#include <QApplication> |
| 10 | +#include <QFileDialog> |
| 11 | +#include <QMessageBox> |
| 12 | +#include <QPixmap> |
| 13 | +#include <QKeyEvent> |
| 14 | +#include <QDebug> |
| 15 | +#include "mainwindow.h" |
| 16 | + |
| 17 | +MainWindow::MainWindow(QWidget* parent) : |
| 18 | + QMainWindow(parent) |
| 19 | + , fileMenu(nullptr) |
| 20 | + , viewMenu(nullptr) |
| 21 | + , currentImage(nullptr) |
| 22 | +{ |
| 23 | + initUI(); |
| 24 | +} |
| 25 | + |
| 26 | +MainWindow::~MainWindow() |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +void MainWindow::initUI() |
| 31 | +{ |
| 32 | + this->resize(800, 600); |
| 33 | + // setup menubar |
| 34 | + fileMenu = menuBar()->addMenu("&File"); |
| 35 | + viewMenu = menuBar()->addMenu("&View"); |
| 36 | + |
| 37 | + // setup toolbar |
| 38 | + fileToolBar = addToolBar("File"); |
| 39 | + viewToolBar = addToolBar("View"); |
| 40 | + |
| 41 | + // main area for image display |
| 42 | + imageScene = new QGraphicsScene(this); |
| 43 | + imageView = new QGraphicsView(imageScene); |
| 44 | + setCentralWidget(imageView); |
| 45 | + |
| 46 | + // setup status bar |
| 47 | + mainStatusBar = statusBar(); |
| 48 | + mainStatusLabel = new QLabel(mainStatusBar); |
| 49 | + mainStatusBar->addPermanentWidget(mainStatusLabel); |
| 50 | + mainStatusLabel->setText("Image Information will be here!"); |
| 51 | + |
| 52 | + createActions(); |
| 53 | +} |
| 54 | + |
| 55 | +void MainWindow::createActions() |
| 56 | +{ |
| 57 | + // create actions, add them to menus |
| 58 | + openAction = new QAction("&Open", this); |
| 59 | + fileMenu->addAction(openAction); |
| 60 | + saveAsAction = new QAction("&Save as", this); |
| 61 | + fileMenu->addAction(saveAsAction); |
| 62 | + exitAction = new QAction("E&xit", this); |
| 63 | + fileMenu->addAction(exitAction); |
| 64 | + |
| 65 | + zoomInAction = new QAction("Zoom in", this); |
| 66 | + viewMenu->addAction(zoomInAction); |
| 67 | + zoomOutAction = new QAction("Zoom Out", this); |
| 68 | + viewMenu->addAction(zoomOutAction); |
| 69 | + prevAction = new QAction("&Previous Image", this); |
| 70 | + viewMenu->addAction(prevAction); |
| 71 | + nextAction = new QAction("&Next Image", this); |
| 72 | + viewMenu->addAction(nextAction); |
| 73 | + |
| 74 | + // add actions to toolbars |
| 75 | + fileToolBar->addAction(openAction); |
| 76 | + viewToolBar->addAction(zoomInAction); |
| 77 | + viewToolBar->addAction(zoomOutAction); |
| 78 | + viewToolBar->addAction(prevAction); |
| 79 | + viewToolBar->addAction(nextAction); |
| 80 | + |
| 81 | + // connect signals and slots |
| 82 | + connect(exitAction, SIGNAL(triggered(bool)), QApplication::instance(), SLOT(quit())); |
| 83 | + connect(openAction, SIGNAL(triggered(bool)), this, SLOT(openImage())); |
| 84 | + connect(saveAsAction, SIGNAL(triggered(bool)), this, SLOT(saveAs())); |
| 85 | + connect(zoomInAction, SIGNAL(triggered(bool)), this, SLOT(zoomIn())); |
| 86 | + connect(zoomOutAction, SIGNAL(triggered(bool)), this, SLOT(zoomOut())); |
| 87 | + connect(prevAction, SIGNAL(triggered(bool)), this, SLOT(prevImage())); |
| 88 | + connect(nextAction, SIGNAL(triggered(bool)), this, SLOT(nextImage())); |
| 89 | + |
| 90 | + setupShortcuts(); |
| 91 | +} |
| 92 | + |
| 93 | +void MainWindow::openImage() |
| 94 | +{ |
| 95 | + QFileDialog dialog(this); |
| 96 | + dialog.setWindowTitle("Open Image"); |
| 97 | + dialog.setFileMode(QFileDialog::ExistingFile); |
| 98 | + dialog.setNameFilter(tr("Images (*.png *.bmp *.jpg)")); |
| 99 | + QStringList filePaths; |
| 100 | + if (dialog.exec()) { |
| 101 | + filePaths = dialog.selectedFiles(); |
| 102 | + showImage(filePaths.at(0)); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +void MainWindow::showImage(QString path) |
| 108 | +{ |
| 109 | + imageScene->clear(); |
| 110 | + imageView->resetMatrix(); |
| 111 | + QPixmap image(path); |
| 112 | + currentImage = imageScene->addPixmap(image); |
| 113 | + imageScene->update(); |
| 114 | + imageView->setSceneRect(image.rect()); |
| 115 | + QString status = QString("%1, %2x%3, %4 Bytes").arg(path).arg(image.width()) |
| 116 | + .arg(image.height()).arg(QFile(path).size()); |
| 117 | + mainStatusLabel->setText(status); |
| 118 | + currentImagePath = path; |
| 119 | +} |
| 120 | + |
| 121 | +void MainWindow::zoomIn() |
| 122 | +{ |
| 123 | + imageView->scale(1.2, 1.2); |
| 124 | +} |
| 125 | + |
| 126 | +void MainWindow::zoomOut() |
| 127 | +{ |
| 128 | + imageView->scale(1 / 1.2, 1 / 1.2); |
| 129 | +} |
| 130 | + |
| 131 | +void MainWindow::prevImage() |
| 132 | +{ |
| 133 | + QFileInfo current(currentImagePath); |
| 134 | + QDir dir = current.absoluteDir(); |
| 135 | + QStringList nameFilters; |
| 136 | + nameFilters << "*.png" << "*.bmp" << "*.jpg"; |
| 137 | + QStringList fileNames = dir.entryList(nameFilters, QDir::Files, QDir::Name); |
| 138 | + int idx = fileNames.indexOf(QRegExp(QRegExp::escape(current.fileName()))); |
| 139 | + if (idx > 0) { |
| 140 | + showImage(dir.absoluteFilePath(fileNames.at(idx - 1))); |
| 141 | + } |
| 142 | + else { |
| 143 | + QMessageBox::information(this, "Information", "Current image is the first one."); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +void MainWindow::nextImage() |
| 148 | +{ |
| 149 | + QFileInfo current(currentImagePath); |
| 150 | + QDir dir = current.absoluteDir(); |
| 151 | + QStringList nameFilters; |
| 152 | + nameFilters << "*.png" << "*.bmp" << "*.jpg"; |
| 153 | + QStringList fileNames = dir.entryList(nameFilters, QDir::Files, QDir::Name); |
| 154 | + int idx = fileNames.indexOf(QRegExp(QRegExp::escape(current.fileName()))); |
| 155 | + if (idx < fileNames.size() - 1) { |
| 156 | + showImage(dir.absoluteFilePath(fileNames.at(idx + 1))); |
| 157 | + } |
| 158 | + else { |
| 159 | + QMessageBox::information(this, "Information", "Current image is the last one."); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +void MainWindow::saveAs() |
| 164 | +{ |
| 165 | + if (currentImage == nullptr) { |
| 166 | + QMessageBox::information(this, "Information", "Nothing to save."); |
| 167 | + return; |
| 168 | + } |
| 169 | + QFileDialog dialog(this); |
| 170 | + dialog.setWindowTitle("Save Image As ..."); |
| 171 | + dialog.setFileMode(QFileDialog::AnyFile); |
| 172 | + dialog.setAcceptMode(QFileDialog::AcceptSave); |
| 173 | + dialog.setNameFilter(tr("Images (*.png *.bmp *.jpg)")); |
| 174 | + QStringList fileNames; |
| 175 | + if (dialog.exec()) { |
| 176 | + fileNames = dialog.selectedFiles(); |
| 177 | + if (QRegExp(".+\\.(png|bmp|jpg)").exactMatch(fileNames.at(0))) { |
| 178 | + currentImage->pixmap().save(fileNames.at(0)); |
| 179 | + } |
| 180 | + else { |
| 181 | + QMessageBox::information(this, "Information", "Save error: bad format or filename."); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +void MainWindow::setupShortcuts() |
| 188 | +{ |
| 189 | + QList<QKeySequence> shortcuts; |
| 190 | + shortcuts << Qt::Key_Plus << Qt::Key_Equal; |
| 191 | + zoomInAction->setShortcuts(shortcuts); |
| 192 | + |
| 193 | + shortcuts.clear(); |
| 194 | + shortcuts << Qt::Key_Minus << Qt::Key_Underscore; |
| 195 | + zoomOutAction->setShortcuts(shortcuts); |
| 196 | + |
| 197 | + shortcuts.clear(); |
| 198 | + shortcuts << Qt::Key_Up << Qt::Key_Left; |
| 199 | + prevAction->setShortcuts(shortcuts); |
| 200 | + |
| 201 | + shortcuts.clear(); |
| 202 | + shortcuts << Qt::Key_Down << Qt::Key_Right; |
| 203 | + nextAction->setShortcuts(shortcuts); |
| 204 | +} |
0 commit comments