CG2_Tasks/imageviewer-qt4.cpp

690 lines
23 KiB
C++
Raw Normal View History

2015-10-23 10:15:11 +02:00
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
2015-10-23 10:40:05 +02:00
#include "imageviewer-qt4.h"
2015-10-23 10:15:11 +02:00
/**
* Constructor
* @brief ImageViewer::ImageViewer
*/
2015-10-23 10:40:05 +02:00
ImageViewer::ImageViewer() {
original = NULL;
working_copy = NULL;
2015-10-23 10:40:05 +02:00
startLogging();
generateMainGui();
renewLogging();
generateControlPanels();
createActions();
createMenus();
resize(1600, 600);
2015-10-23 10:15:11 +02:00
}
/**
* Once an image is loaded, automatically perform these operations.
*
* analyze the image
* allocate brightness map
*
* @brief ImageViewer::initializeImage
*/
void ImageViewer::initializeImage() {
if(original->getImage() == NULL) {
std::cout << "Error! No image provided!" << std::endl;
return;
}
analyzeImage();
}
/**
* In case it is needed, convert the loaded image into grayscale.
*
* @brief ImageViewer::convertToMonochrome
*/
void ImageViewer::convertToMonochrome() {
if(original->getImage() != NULL) {
logFile << "Converting image to monochrome...";
renewLogging();
working_copy->convertToMonochrome();
updateImageDisplay();
logFile << "done." << std::endl;
renewLogging();
}
}
/**
* No big performance, but simple.
* Restore the working copy based on the existing original image.
*
* @brief ImageViewer::resetWorkingCopy
*/
void ImageViewer::resetWorkingCopy() {
logFile << "Resetting working copy ...";
renewLogging();
for(int x=0; x<original->getImage()->width(); x++) {
for(int y=0; y<original->getImage()->height(); y++) {
working_copy->getImage()->setPixel(x, y, original->getImage()->pixel(x, y));
}
}
updateImageDisplay();
logFile << "done." << std::endl;
renewLogging();
}
/**
* Draws a simple, black line.
* @brief ImageViewer::drawBlackLine
*/
2015-10-23 10:40:05 +02:00
void ImageViewer::drawBlackLine() {
if(original->getImage() != NULL) {
for(int i=0;i<std::min(working_copy->getImage()->width(),working_copy->getImage()->height());i++) {
working_copy->getImage()->setPixel(i,i,0);
2015-10-23 10:15:11 +02:00
}
updateImageDisplay();
logFile << "Black line drawn." << std::endl;
renewLogging();
}
}
/**
* Draws a diagonal cross from edge to edge.
* @brief ImageViewer::drawDiagonalCross
*/
2015-10-23 10:15:11 +02:00
void ImageViewer::drawDiagonalCross() {
if(original->getImage() != NULL) {
int color = QColor::fromHsl(120,255,125).rgb();
int width = working_copy->getImage()->width();
int height = working_copy->getImage()->height();
for(int y=0; y<working_copy->getImage()->height(); y++) {
working_copy->getImage()->setPixel((1.0*y/height)*width,y,color);
working_copy->getImage()->setPixel((1.0-(1.0*y/height))*width,y,color);
2015-10-23 10:15:11 +02:00
}
}
updateImageDisplay();
logFile << "Diagonal edge-to-edge cross drawn." << std::endl;
2015-10-23 10:15:11 +02:00
renewLogging();
}
/**
* Proxy method to actual method due to signal param issues.
* @brief ImageViewer::drawRainbowCross
*/
2015-10-23 10:15:11 +02:00
void ImageViewer::drawRainbowCross() {
drawRainbowCross(0);
}
/**
* Draws a diagonal cross with variable width and
* varying color.
* Fetches the width from the slider.
* @brief ImageViewer::drawRainbowCross
* @param initialHue
*/
2015-10-23 10:15:11 +02:00
void ImageViewer::drawRainbowCross(int initialHue=0) {
if(original->getImage() != NULL) {
2015-10-23 10:15:11 +02:00
int h = initialHue;
QColor myColor = QColor::fromHsl(h, 255, 125);
int image_width = working_copy->getImage()->width()-1;
2015-11-06 12:21:36 +01:00
int range = line_slider->value();
if(range > working_copy->getImage()->width()-2) range = working_copy->getImage()->width()-2;
for(int i=0;i<std::min(working_copy->getImage()->width(),working_copy->getImage()->height());i++) {
int color = myColor.rgb();
2015-10-23 10:15:11 +02:00
for(int r=0; r<range; r++) {
working_copy->getImage()->setPixel(i+r,i,color);
working_copy->getImage()->setPixel((image_width-i)-r,i,color);
2015-10-23 10:15:11 +02:00
}
h++;
if(h > 359) h = 0;
myColor.setHsl(h, 255, 125);
}
updateImageDisplay();
}
}
2015-10-30 13:52:23 +01:00
/**
* Analyze the image, get average luminance
* Also fill grayscale_absolute_histogramm
2015-11-13 13:22:48 +01:00
*
2015-10-30 13:52:23 +01:00
* @brief ImageViewer::analyzeImage
*/
void ImageViewer::analyzeImage() {
if(original->getImage() != NULL) {
2015-10-30 13:52:23 +01:00
logFile << "Analyzing image ...";
renewLogging();
original->updateStatistics();
working_copy->updateStatistics();
2015-10-30 13:52:23 +01:00
logFile << "done" << std::endl;
renewLogging();
QString result = QString("Intensity: Average: %1, Variance: %2").arg(working_copy->getIntensityAverage()).arg(working_copy->getIntensityVariance());
stats->setText(result);
histogramm_label->setPixmap(QPixmap::fromImage(*(working_copy->getHistogrammImage())));
original_histogramm_label->setPixmap(QPixmap::fromImage(*(original->getHistogrammImage())));
2015-10-30 13:52:23 +01:00
}
2015-10-30 13:58:51 +01:00
}
/**
* Fired by the brightness_slider, this one adjusts the brightness on image.
* @brief ImageViewer::adjustBrightness
*/
void ImageViewer::adjustBrightness(int b) {
int h, s, old_l;
int new_l = 0;
int delta = b - 255;
for(int x=0; x<working_copy->getImage()->width(); x++) {
for(int y=0; y<working_copy->getImage()->height(); y++) {
QColor color = QColor::fromRgb(original->getImage()->pixel(x, y));
color.getHsl(&h, &s, &old_l);
new_l = old_l + delta;
if(new_l > 255) new_l = 255;
if(new_l < 0) new_l = 0;
color.setHsl(h, s, new_l);
working_copy->getImage()->setPixel(x, y, color.rgb());
}
}
updateImageDisplay();
logFile << "Brightness adjusted to: " << old_l << " + " << delta << " -> (" << h << ", " << s << ", " << new_l << ")" << std::endl;
renewLogging();
}
/**
* Fired by the constrast_slider, this one adjusts the contrast on image.
* @brief ImageViewer::adjustContrast
*/
void ImageViewer::adjustContrast(int c) {
int h, s, old_l;
int new_l = 0;
double alpha = c / 255.0;
int average_intensity = original->getIntensityAverage();
for(int x=0; x<working_copy->getImage()->width(); x++) {
for(int y=0; y<working_copy->getImage()->height(); y++) {
QColor color = QColor::fromRgb(original->getImage()->pixel(x, y));
color.getHsl(&h, &s, &old_l);
new_l = average_intensity + ((int) round((old_l - average_intensity) * alpha));
if(new_l > 255) new_l = 255;
if(new_l < 0) new_l = 0;
color.setHsl(h, s, new_l);
working_copy->getImage()->setPixel(x, y, color.rgb());
}
}
updateImageDisplay();
2015-11-10 13:02:55 +01:00
logFile << "Contrast adjusted to: " << old_l << " * " << alpha << " -> (" << h << ", " << s << ", " << new_l << ")" << std::endl;
renewLogging();
}
/**
* Using the percentile given by the slider, do a robust automatic contrast adaption.
*
* @brief ImageViewer::robustAutomaticContrastAdaption
*/
void ImageViewer::robustAutomaticContrastAdaption(void) {
double* histogramm = original->getRelativeIntensityHistogramm();
double limit = contrast_percentile_slider->value() / 100.0;
logFile << "Doing automatic robust contrast adaption (" << limit << "), hold tight ..." << std::endl;
renewLogging();
// Determine upper and lower "borders"
double cursor;
int lower_border = -1;
int upper_border = -1;
cursor = 0.0;
for(int i=0; i<256; i++) {
cursor += histogramm[i];
if(cursor > limit) {
lower_border = i-1; // get last value before limit overflow
if(lower_border < 0) lower_border = 0;
break;
}
}
cursor = 0.0;
for(int i=255; i>-1; i--) {
cursor += histogramm[i];
if(cursor > limit) {
upper_border = i+1; // get last value before limit overflow
if(upper_border > 255) upper_border = 255;
break;
}
}
logFile << "lower border: " << lower_border << ", upper border: " << upper_border << "." << std::endl;
renewLogging();
int h, s, l;
for(int x=0; x<working_copy->getImage()->width(); x++) {
for(int y=0; y<working_copy->getImage()->height(); y++) {
QColor color = QColor::fromRgb(original->getImage()->pixel(x, y));
color.getHsl(&h, &s, &l);
if(l < lower_border) {
l = 0;
} else if(l > upper_border) {
l = 255;
} else {
l = qRound((l - lower_border) * (255.0/(upper_border-lower_border)));
}
color.setHsl(h, s, l);
working_copy->getImage()->setPixel(x, y, color.rgb());
}
}
updateImageDisplay();
logFile << "done." << std::endl;
renewLogging();
}
2015-10-23 10:15:11 +02:00
/****************************************************************************************
*
* GUI elements related to the tasks are set up here.
2015-10-23 10:15:11 +02:00
*
*****************************************************************************************/
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::generateControlPanels() {
// Tab for first task
task_tab_widget1 = new QWidget();
task_tab1 = new QVBoxLayout();
task_tab_widget1->setLayout(task_tab1);
2015-10-23 10:40:05 +02:00
monochrome = new QPushButton();
monochrome->setText("Convert to monochrome");
QObject::connect(monochrome, SIGNAL(clicked()), this, SLOT (convertToMonochrome()));
reset_working_copy = new QPushButton();
reset_working_copy->setText("Reset working copy");
QObject::connect(reset_working_copy, SIGNAL(clicked()), this, SLOT (resetWorkingCopy()));
draw_black_line = new QPushButton();
draw_black_line->setText("Draw a black line");
QObject::connect(draw_black_line, SIGNAL(clicked()), this, SLOT (drawBlackLine()));
2015-10-23 10:40:05 +02:00
draw_rainbow_cross = new QPushButton();
draw_rainbow_cross->setText("Draw a rainbow cross");
QObject::connect(draw_rainbow_cross, SIGNAL(clicked()), this, SLOT (drawRainbowCross()));
2015-10-23 10:40:05 +02:00
diagonal_cross = new QPushButton();
diagonal_cross->setText("Draw a diagonal cross");
QObject::connect(diagonal_cross, SIGNAL(clicked()), this, SLOT (drawDiagonalCross()));
2015-10-23 10:40:05 +02:00
2015-11-06 12:21:36 +01:00
line_slider = new QSlider(Qt::Horizontal);
line_slider->setMinimum(1);
line_slider->setMaximum(150);
line_slider->setValue(3);
//QObject::connect(lineSlider, SIGNAL(valueChanged(int)), this, SLOT (drawRainbowCross()));
task_tab1->addWidget(reset_working_copy);
task_tab1->addWidget(monochrome);
task_tab1->addWidget(new QLabel("Let's draw something!"));
task_tab1->addWidget(draw_black_line);
task_tab1->addWidget(draw_rainbow_cross);
task_tab1->addWidget(diagonal_cross);
task_tab1->addWidget(new QLabel("This will not stop unless process is killed."));
2015-11-06 12:21:36 +01:00
task_tab1->addWidget(line_slider);
task_tab1->addWidget(new QLabel("Sets the width of the cross."));
2015-11-06 12:15:41 +01:00
tabWidget->addTab(task_tab_widget1, "Task #1");
//Tab for second task
2015-11-06 12:15:41 +01:00
task_tab_widget2 = new QWidget();
task_tab2 = new QVBoxLayout();
task_tab_widget2->setLayout(task_tab2);
original_histogramm_label = new QLabel();
original_histogramm_label->setBackgroundRole(QPalette::Base);
original_histogramm_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
original_histogramm_label->setScaledContents(false);
2015-11-06 12:21:36 +01:00
histogramm_label = new QLabel();
histogramm_label->setBackgroundRole(QPalette::Base);
histogramm_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
2015-11-06 12:21:36 +01:00
histogramm_label->setScaledContents(false);
stats = new QLabel();
stats->setText("Intensity will be shown here.");
2015-11-06 12:21:36 +01:00
analyze_image = new QPushButton();
analyze_image->setText("Update image statistics, redraw histogramms.");
QObject::connect(analyze_image, SIGNAL(clicked()), this, SLOT(analyzeImage()));
2015-11-06 12:21:36 +01:00
brightness_slider = new QSlider(Qt::Horizontal);
brightness_slider->setMinimum(0);
brightness_slider->setMaximum(510);
brightness_slider->setValue(255);
QObject::connect(brightness_slider, SIGNAL(valueChanged(int)), this, SLOT (adjustBrightness(int)));
2015-11-06 12:21:36 +01:00
contrast_slider = new QSlider(Qt::Horizontal);
contrast_slider->setMinimum(0);
contrast_slider->setMaximum(510);
contrast_slider->setValue(255);
QObject::connect(contrast_slider, SIGNAL(valueChanged(int)), this, SLOT (adjustContrast(int)));
contrast_percentile_slider = new QSlider(Qt::Horizontal);
contrast_percentile_slider->setMinimum(0);
2015-11-13 13:22:48 +01:00
contrast_percentile_slider->setMaximum(50);
contrast_percentile_slider->setValue(5);
automatic_robust_contrast = new QPushButton("Automatic robust contrast adaption");
QObject::connect(automatic_robust_contrast, SIGNAL(clicked()), this, SLOT(robustAutomaticContrastAdaption()));
task_tab2->addWidget(stats);
task_tab2->addWidget(new QLabel("Histogramm of original image"));
task_tab2->addWidget(original_histogramm_label);
task_tab2->addWidget(new QLabel("Histogramm of working copy"));
2015-11-06 12:21:36 +01:00
task_tab2->addWidget(histogramm_label);
task_tab2->addWidget(analyze_image);
2015-11-06 12:21:36 +01:00
task_tab2->addWidget(new QLabel("Brightness"));
task_tab2->addWidget(brightness_slider);
task_tab2->addWidget(new QLabel("Contrast"));
task_tab2->addWidget(contrast_slider);
task_tab2->addWidget(new QLabel("Automatic Contrast Percentile"));
task_tab2->addWidget(contrast_percentile_slider);
task_tab2->addWidget(automatic_robust_contrast);
2015-10-23 10:15:11 +02:00
2015-11-06 12:15:41 +01:00
tabWidget->addTab(task_tab_widget2, "Task #2");
//Tab for third task
task_tab_widget3 = new QWidget();
task_tab3 = new QVBoxLayout();
task_tab_widget3->setLayout(task_tab3);
tabWidget->addTab(task_tab_widget3, "Task #3");
//Show it
2015-10-30 13:52:23 +01:00
tabWidget->show();
2015-10-23 10:15:11 +02:00
}
2015-10-23 10:15:11 +02:00
/****************************************************************************************
*
* The following methods are about the adjustBrightness(int)generic image viewer application and do not contain
* any special graphics logic.
2015-10-23 10:15:11 +02:00
*
*****************************************************************************************/
void ImageViewer::startLogging()
{
2015-10-23 10:40:05 +02:00
//LogFile
logFile.open("log.txt", std::ios::out);
logFile << "Logging: \n" << std::endl;
2015-10-23 10:15:11 +02:00
}
void ImageViewer::renewLogging()
{
2015-10-23 10:40:05 +02:00
QFile file("log.txt"); // Create a file handle for the file named
QString line;
file.open(QIODevice::ReadOnly); // Open the file
QTextStream stream(&file); // Set the stream to read from myFile
logBrowser->clear();
while(!stream.atEnd()) {
line = stream.readLine(); // this reads a line (QString) from the file
logBrowser->append(line);
}
2015-10-23 10:15:11 +02:00
}
2015-10-23 10:40:05 +02:00
void ImageViewer::updateImageDisplay() {
imageLabel->setPixmap(QPixmap::fromImage(*(working_copy->getImage())));
2015-10-23 10:15:11 +02:00
}
2015-10-23 10:40:05 +02:00
void ImageViewer::generateMainGui() {
/* Tab widget */
tabWidget = new QTabWidget(this);
tabWidget->setObjectName("tabWidget");
/* Center widget */
centralwidget = new QWidget(this);
centralwidget->setObjectName("centralwidget");
//centralwidget->setFixedSize(200,200);
//setCentralWidget(centralwidget);
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
/* Center widget */
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
setCentralWidget(scrollArea);
/* HBox layout */
QGridLayout* gLayout = new QGridLayout(centralwidget);
gLayout->setObjectName("hboxLayout");
gLayout->addWidget(new QLabel(),1,1);
gLayout->setVerticalSpacing(50);
gLayout->addWidget(tabWidget,2,1);
gLayout->addWidget(scrollArea,2,2);
logBrowser= new QTextEdit(this);
logBrowser->setMinimumHeight(100);
logBrowser->setMaximumHeight(200);
logBrowser->setMinimumWidth(width());
logBrowser->setMaximumWidth(width());
gLayout->addWidget(logBrowser,3,1,1,2);
gLayout->setVerticalSpacing(50);
2015-10-23 10:15:11 +02:00
}
2015-10-23 10:40:05 +02:00
void ImageViewer::print() {
Q_ASSERT(imageLabel->pixmap());
#if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
QPrintDialog dialog(&printer, this);
if (dialog.exec()) {
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = imageLabel->pixmap()->size();
size.scale(rect.size(), Qt::KeepAspectRatio);
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(imageLabel->pixmap()->rect());
painter.drawPixmap(0, 0, *imageLabel->pixmap());
}
#endif
2015-10-23 10:15:11 +02:00
}
2015-10-23 10:40:05 +02:00
void ImageViewer::open() {
if(working_copy != NULL) {
delete working_copy;
working_copy = NULL;
2015-10-23 10:40:05 +02:00
}
if(original != NULL) {
delete original;
original = NULL;
}
2015-10-23 10:40:05 +02:00
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
if(!fileName.isEmpty()) {
working_copy = new LazyImage(new QImage(fileName));
original = new LazyImage(new QImage(fileName));
if(working_copy->getImage()->isNull()) {
2015-10-23 10:40:05 +02:00
QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
return;
}
2015-10-23 10:15:11 +02:00
scaleFactor = 1.0;
updateImageDisplay();
printAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
2015-10-23 10:40:05 +02:00
if(!fitToWindowAct->isChecked()) imageLabel->adjustSize();
2015-10-23 10:15:11 +02:00
setWindowFilePath(fileName);
logFile << "geladen: " << fileName.toStdString().c_str() << std::endl;
renewLogging();
initializeImage(); //Now hook to prepare certain datastructures after loading.
2015-10-23 10:40:05 +02:00
}
2015-10-23 10:15:11 +02:00
}
2015-10-23 10:40:05 +02:00
void ImageViewer::resizeEvent(QResizeEvent* event) {
QMainWindow::resizeEvent(event);
centralwidget->setMinimumWidth(width());
centralwidget->setMinimumHeight(height());
centralwidget->setMaximumWidth(width());
centralwidget->setMaximumHeight(height());
logBrowser->setMinimumWidth(width()-40);
logBrowser->setMaximumWidth(width()-40);
2015-10-23 10:15:11 +02:00
}
2015-10-23 10:40:05 +02:00
void ImageViewer::zoomIn() {
scaleImage(1.25);
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::zoomOut() {
scaleImage(0.8);
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::normalSize() {
imageLabel->adjustSize();
scaleFactor = 1.0;
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::fitToWindow() {
bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow);
if(!fitToWindow) {
normalSize();
}
updateActions();
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::about() {
QMessageBox::about(this, tr("About Image Viewer"),
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
"and QScrollArea to display an image. QLabel is typically used "
"for displaying a text, but it can also display an image. "
"QScrollArea provides a scrolling view around another widget. "
"If the child widget exceeds the size of the frame, QScrollArea "
"automatically provides scroll bars. </p><p>The example "
"demonstrates how QLabel's ability to scale its contents "
"(QLabel::scaledContents), and QScrollArea's ability to "
"automatically resize its contents "
"(QScrollArea::widgetResizable), can be used to implement "
"zooming and scaling features. </p><p>In addition the example "
"shows how to use QPainter to print an image.</p>"));
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::createActions() {
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
printAct = new QAction(tr("&Print..."), this);
printAct->setShortcut(tr("Ctrl+P"));
printAct->setEnabled(false);
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
zoomInAct->setShortcut(tr("Ctrl++"));
zoomInAct->setEnabled(false);
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
zoomOutAct->setShortcut(tr("Ctrl+-"));
zoomOutAct->setEnabled(false);
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
normalSizeAct = new QAction(tr("&Normal Size"), this);
normalSizeAct->setShortcut(tr("Ctrl+S"));
normalSizeAct->setEnabled(false);
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
fitToWindowAct = new QAction(tr("&Fit to Window"), this);
fitToWindowAct->setEnabled(false);
fitToWindowAct->setCheckable(true);
fitToWindowAct->setShortcut(tr("Ctrl+F"));
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::createMenus() {
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct);
fileMenu->addAction(printAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
viewMenu = new QMenu(tr("&View"), this);
viewMenu->addAction(zoomInAct);
viewMenu->addAction(zoomOutAct);
viewMenu->addAction(normalSizeAct);
viewMenu->addSeparator();
viewMenu->addAction(fitToWindowAct);
helpMenu = new QMenu(tr("&Help"), this);
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
menuBar()->addMenu(fileMenu);
menuBar()->addMenu(viewMenu);
menuBar()->addMenu(helpMenu);
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::updateActions() {
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::scaleImage(double factor) {
Q_ASSERT(imageLabel->pixmap());
scaleFactor *= factor;
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
zoomInAct->setEnabled(scaleFactor < 3.0);
zoomOutAct->setEnabled(scaleFactor > 0.333);
}
2015-10-23 10:15:11 +02:00
2015-10-23 10:40:05 +02:00
void ImageViewer::adjustScrollBar(QScrollBar* scrollBar, double factor) {
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
}