-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainwindow.cpp
138 lines (116 loc) · 3.9 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "aboutdlg.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QFileInfo>
#include <QDebug>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_infoLabel = new QLabel(this);
m_infoLabel->setStyleSheet("QLabel {padding-left:3px;}");
this->setStyleSheet("QStatusBar::item {border:none;}");
// statusbar info left (color settings)
QPalette palette;
palette.setColor( QPalette::WindowText,"#cccccc");
ui->statusBar->setPalette(palette);
ui->statusBar->addWidget(m_infoLabel);
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openImage()));
connect(ui->actionClear, SIGNAL(triggered()), this, SLOT(closeImage()));
connect(ui->actionFitWindow, SIGNAL(toggled(bool)), ui->graphicsView, SLOT(reactToFitWindowToggle(bool)));
connect(ui->actionPrint, SIGNAL(triggered()), this, SLOT(printImage()));
connect(ui->actionSave,SIGNAL(triggered()), this, SLOT(saveImage()));
connect(ui->actionRotate_Left, SIGNAL(triggered()), this, SLOT(rotateImage()));
connect(ui->actionRotate_right, SIGNAL(triggered()), this, SLOT(rotateImage()));
enableControls(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::enableControls(bool bEnable)
{
ui->actionClear->setEnabled(bEnable);
ui->actionPrint->setEnabled(bEnable);
ui->actionRotate_Left->setEnabled(bEnable);
ui->actionRotate_right->setEnabled(bEnable);
ui->actionSave->setEnabled(bEnable);
ui->actionFitWindow->setEnabled(bEnable);
}
void MainWindow::openImage()
{
QString strFilePath = QFileDialog::getOpenFileName(
this,
tr("Open File"),
QDir::homePath(),
tr("Images (*.png *.jpg *.bmp *.tiff *.tif)"));
if (!strFilePath.isEmpty()){
QApplication::setOverrideCursor(Qt::WaitCursor);
QString strError;
if(!ui->graphicsView->loadFile(strFilePath,strError) ){
QApplication::restoreOverrideCursor();
QMessageBox::information(this,tr("Error"),tr(strError.toLocal8Bit().constData())); //"Error displaying image."
return;
}
QApplication::restoreOverrideCursor();
updateStatusBarInfo(strFilePath);
enableControls(true);
}
}
void MainWindow::printImage()
{
ui->graphicsView->printView();
}
void MainWindow::closeImage()
{
ui->graphicsView->resetView();
m_infoLabel->setText("");
enableControls(false);
}
void MainWindow::saveImage()
{
QString strError;
if(!ui->graphicsView->saveViewToDisk(strError)){
QApplication::restoreOverrideCursor();
if(!strError.isEmpty())
QMessageBox::information(this,tr("Error"),strError);
return;
}
}
void MainWindow::rotateImage()
{
QObject* obj = sender();
if(obj == ui->actionRotate_Left)
ui->graphicsView->rotateView(-90);
else
ui->graphicsView->rotateView(90);
}
QString MainWindow::formatByteSize(int nBytes)
{
int B = 1;
int KB = 1024 * B;
int MB = 1024 * KB;
int GB = 1024 * MB;
QString str;
if(nBytes > GB) str = QString::number(nBytes/GB, 'f', 2) + " GB";
else if(nBytes > MB) str = QString::number(nBytes/MB, 'f', 2) + " MB";
else if(nBytes > KB) str = QString::number(nBytes/KB, 'f', 2) + " KB";
else str = QString::number(nBytes, 'f', 2) + " B";
return str;
}
void MainWindow::updateStatusBarInfo(QString strFile)
{
QFileInfo imgInfo(strFile);
QDateTime dtLastModified = imgInfo.lastModified();
QString strStatusInfo = QString(strFile+" "+"~%1"+" "+"%2").arg(formatByteSize(imgInfo.size())).arg(dtLastModified.toString("d/M/yyyy hh:mm:ss"));
m_infoLabel->setText(strStatusInfo);
}
void MainWindow::on_actionAbout_triggered()
{
AboutDlg dlg(this);
dlg.exec();
}