#include <QtGui>
#include <QImageWriter>
#include "imageviewer.h"
ImageViewer::ImageViewer()
{
imageLabel
->setBackgroundRole
(QPalette::Base);
imageLabel->setScaledContents(true);
scrollArea
->setBackgroundRole
(QPalette::Mid);
scrollArea->setWidget(imageLabel);
setCentralWidget(scrollArea);
createActions();
createMenus();
setWindowTitle(tr("Obdelava in Analiza slik"));
resize(500, 400);
}
void ImageViewer::open()
{
QList<QByteArray> formats
= QImageWriter::supportedImageFormats();
{
filter
+= QString("%1 files (*.%2);;").
arg(format.
toUpper()).
arg(format
);
}
// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}
QString fileName
= QFileDialog::getOpenFileName(this,tr
("Open File"),
QDir::currentPath(), filter,
&selectedFilter
);
if (!fileName.isEmpty())
{
image.load(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr
("ImageViewer"), tr
("Cannot load %1.").
arg(fileName
));
return;
}
imageLabel
->setPixmap
(QPixmap::fromImage(image
));
scaleFactor = 1.0;
saveAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
}
void ImageViewer::save()
{
// construct a filter of all supported formats
QList<QByteArray> formats
= QImageWriter::supportedImageFormats();
{
filter
+= QString("%1 files (*.%2);;").
arg(format.
toUpper()).
arg(format
);
}
// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}
// get save file name
QString s
= QFileDialog::getSaveFileName(this,
"Save image as",
QDir::currentPath(), filter,
&selectedFilter
);
if (!s.isEmpty())
{
// check for the selected format
QString format
= selectedFilter.
split(" ").
at(0);
if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
s.chop(fi.suffix().length());
// set correct suffix
s += "." + format.toLower();
}
// save image in the selected format
if (!image.save(s, format.toAscii().constData()))
{
}
}
}
void ImageViewer::zoomIn()
{
scaleImage(1.25);
}
void ImageViewer::zoomOut()
{
scaleImage(0.8);
}
void ImageViewer::normalSize()
{
imageLabel->adjustSize();
scaleFactor = 1.0;
}
void ImageViewer::fitToWindow()
{
bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow)
{
normalSize();
}
updateActions();
}
void ImageViewer::about()
{
tr("<h2>Obdelava in Analiza slik</h2>");
}
void ImageViewer::createActions()
{
openAct
= new QAction(tr
("&Open..."),
this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct
= new QAction(tr
("&Save..."),
this);
saveAct->setShortcut(tr("Ctrl+S"));
saveAct->setEnabled(false);
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
reloadAct
= new QAction(tr
("&Reload Image..."),
this);
reloadAct->setShortcut(tr("Ctrl+R"));
connect(reloadAct, SIGNAL(triggered()), this, SLOT(reload()));
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()));
bwAct
= new QAction(tr
("Black &White"),
this);
bwAct->setShortcut(tr("Ctrl+B"));
connect(bwAct, SIGNAL(triggered()), this, SLOT(bw()));
medianaAct
= new QAction(tr
("&Mediana"),
this);
medianaAct->setShortcut(tr("Ctrl+M"));
connect(medianaAct, SIGNAL(triggered()), this, SLOT(mediana()));
aboutAct
= new QAction(tr
("&About"),
this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}
void ImageViewer::createMenus()
{
fileMenu
= new QMenu(tr
("&File"),
this);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(reloadAct);
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);
filterMenu
= new QMenu(tr
("&Filters"),
this);
filterMenu->addAction(bwAct);
filterMenu->addAction(medianaAct);
helpMenu
= new QMenu(tr
("&Help"),
this);
helpMenu->addAction(aboutAct);
menuBar()->addMenu(fileMenu);
menuBar()->addMenu(viewMenu);
menuBar()->addMenu(filterMenu);
menuBar()->addMenu(helpMenu);
}
void ImageViewer::bw()
{
}
void ImageViewer::mediana()
{
}
void ImageViewer::reload()
{
image.load(fileName);
imageLabel
->setPixmap
(QPixmap::fromImage(image
));
scaleFactor = 1.0;
saveAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
void ImageViewer::updateActions()
{
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}
void ImageViewer::scaleImage(double factor)
{
Q_ASSERT(imageLabel->pixmap());
scaleFactor *= factor;
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
zoomInAct->setEnabled(scaleFactor < 3.0);
zoomOutAct->setEnabled(scaleFactor > 0.333);
}
void ImageViewer
::adjustScrollBar(QScrollBar *scrollBar,
double factor
) {
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
}
#include <QtGui>
#include <QImageWriter>
#include "imageviewer.h"
ImageViewer::ImageViewer()
{
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Mid);
scrollArea->setWidget(imageLabel);
setCentralWidget(scrollArea);
createActions();
createMenus();
setWindowTitle(tr("Obdelava in Analiza slik"));
resize(500, 400);
}
void ImageViewer::open()
{
QString filter;
QString selectedFilter;
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
foreach (QString format, formats)
{
filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
}
// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath(), filter, &selectedFilter);
if (!fileName.isEmpty())
{
image.load(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("ImageViewer"), tr("Cannot load %1.").arg(fileName));
return;
}
imageLabel->setPixmap(QPixmap::fromImage(image));
scaleFactor = 1.0;
saveAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
}
void ImageViewer::save()
{
// construct a filter of all supported formats
QString filter;
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
foreach (QString format, formats)
{
filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
}
// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}
// get save file name
QString selectedFilter;
QString s = QFileDialog::getSaveFileName(this, "Save image as", QDir::currentPath(), filter, &selectedFilter);
if (!s.isEmpty())
{
// check for the selected format
QString format = selectedFilter.split(" ").at(0);
QFileInfo fi(s);
if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
s.chop(fi.suffix().length());
// set correct suffix
s += "." + format.toLower();
}
// save image in the selected format
if (!image.save(s, format.toAscii().constData()))
{
QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(s));
}
}
}
void ImageViewer::zoomIn()
{
scaleImage(1.25);
}
void ImageViewer::zoomOut()
{
scaleImage(0.8);
}
void ImageViewer::normalSize()
{
imageLabel->adjustSize();
scaleFactor = 1.0;
}
void ImageViewer::fitToWindow()
{
bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow)
{
normalSize();
}
updateActions();
}
void ImageViewer::about()
{
QMessageBox::about(this, tr("About"),
tr("<h2>Obdelava in Analiza slik</h2>");
}
void ImageViewer::createActions()
{
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(tr("&Save..."), this);
saveAct->setShortcut(tr("Ctrl+S"));
saveAct->setEnabled(false);
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
reloadAct = new QAction(tr("&Reload Image..."), this);
reloadAct->setShortcut(tr("Ctrl+R"));
connect(reloadAct, SIGNAL(triggered()), this, SLOT(reload()));
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()));
bwAct = new QAction(tr("Black &White"), this);
bwAct->setShortcut(tr("Ctrl+B"));
connect(bwAct, SIGNAL(triggered()), this, SLOT(bw()));
medianaAct = new QAction(tr("&Mediana"), this);
medianaAct->setShortcut(tr("Ctrl+M"));
connect(medianaAct, SIGNAL(triggered()), this, SLOT(mediana()));
aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}
void ImageViewer::createMenus()
{
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(reloadAct);
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);
filterMenu = new QMenu(tr("&Filters"), this);
filterMenu->addAction(bwAct);
filterMenu->addAction(medianaAct);
helpMenu = new QMenu(tr("&Help"), this);
helpMenu->addAction(aboutAct);
menuBar()->addMenu(fileMenu);
menuBar()->addMenu(viewMenu);
menuBar()->addMenu(filterMenu);
menuBar()->addMenu(helpMenu);
}
void ImageViewer::bw()
{
}
void ImageViewer::mediana()
{
}
void ImageViewer::reload()
{
image.load(fileName);
imageLabel->setPixmap(QPixmap::fromImage(image));
scaleFactor = 1.0;
saveAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
void ImageViewer::updateActions()
{
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}
void ImageViewer::scaleImage(double factor)
{
Q_ASSERT(imageLabel->pixmap());
scaleFactor *= factor;
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
zoomInAct->setEnabled(scaleFactor < 3.0);
zoomOutAct->setEnabled(scaleFactor > 0.333);
}
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
}
To copy to clipboard, switch view to plain text mode
Bookmarks