class Ui_MainWindow
{
public:
{
MainWindow
->setObjectName
(QString::fromUtf8("MainWindow"));
MainWindow
->resize
(QSize(616,
667).
expandedTo(MainWindow
->minimumSizeHint
()));
actionOpen
= new QAction(MainWindow
);
actionOpen
->setObjectName
(QString::fromUtf8("actionOpen"));
actionZoomIn
= new QAction(MainWindow
);
actionZoomIn
->setObjectName
(QString::fromUtf8("actionZoomIn"));
actionZoomOut
= new QAction(MainWindow
);
actionZoomOut
->setObjectName
(QString::fromUtf8("actionZoomOut"));
actionZoomNormal
= new QAction(MainWindow
);
actionZoomNormal
->setObjectName
(QString::fromUtf8("actionZoomNormal"));
actionFitToWindow
= new QAction(MainWindow
);
actionFitToWindow
->setObjectName
(QString::fromUtf8("actionFitToWindow"));
centralwidget
= new QWidget(MainWindow
);
centralwidget
->setObjectName
(QString::fromUtf8("centralwidget"));
MainWindow->setCentralWidget(centralwidget);
menubar
->setObjectName
(QString::fromUtf8("menubar"));
menubar
->setGeometry
(QRect(0,
0,
516,
19));
menuFile
= new QMenu(menubar
);
menuFile
->setObjectName
(QString::fromUtf8("menuFile"));
MainWindow->setMenuBar(menubar);
statusbar
->setObjectName
(QString::fromUtf8("statusbar"));
statusbar
->setGeometry
(QRect(0,
548,
516,
19));
MainWindow->setStatusBar(statusbar);
menubar->addAction(menuFile->menuAction());
menuFile->addAction(actionOpen);
menuFile->addAction(actionZoomIn);
menuFile->addAction(actionZoomOut);
menuFile->addAction(actionZoomNormal);
menuFile->addAction(actionFitToWindow);
class Ui_MainWindow
{
public:
QAction *actionOpen;
QAction *actionZoomIn;
QAction *actionZoomOut;
QAction *actionZoomNormal;
QAction *actionFitToWindow;
QWidget *centralwidget;
QMenuBar *menubar;
QMenu *menuFile;
QStatusBar *statusbar;
void setupUi(QMainWindow *MainWindow)
{
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(QSize(616, 667).expandedTo(MainWindow->minimumSizeHint()));
actionOpen = new QAction(MainWindow);
actionOpen->setObjectName(QString::fromUtf8("actionOpen"));
actionZoomIn = new QAction(MainWindow);
actionZoomIn->setObjectName(QString::fromUtf8("actionZoomIn"));
actionZoomOut = new QAction(MainWindow);
actionZoomOut->setObjectName(QString::fromUtf8("actionZoomOut"));
actionZoomNormal = new QAction(MainWindow);
actionZoomNormal->setObjectName(QString::fromUtf8("actionZoomNormal"));
actionFitToWindow = new QAction(MainWindow);
actionFitToWindow->setObjectName(QString::fromUtf8("actionFitToWindow"));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 516, 19));
menuFile = new QMenu(menubar);
menuFile->setObjectName(QString::fromUtf8("menuFile"));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
statusbar->setGeometry(QRect(0, 548, 516, 19));
MainWindow->setStatusBar(statusbar);
menubar->addAction(menuFile->menuAction());
menuFile->addAction(actionOpen);
menuFile->addAction(actionZoomIn);
menuFile->addAction(actionZoomOut);
menuFile->addAction(actionZoomNormal);
menuFile->addAction(actionFitToWindow);
To copy to clipboard, switch view to plain text mode
#include "imagezoomer.h"
#include <QtGui>
#include <qdir.h>
#include <QColor>
ImageZoomer::ImageZoomer(Ui::MainWindow *_mwin): mwin(_mwin)
{
frame
= new QFrame(mwin
->centralwidget
);
frame
->setGeometry
(QRect(60,
70,
591,
571));
frame
->setFrameShape
(QFrame::StyledPanel);
frame
->setFrameShadow
(QFrame::Plain);
canvas = new Q3Canvas(frame);
//canview = new Q3CanvasView(frame);
//canview->setCanvas(canvas);
canvas->setBackgroundColor(Qt::black);
canvas->resize(400,400);
canview = new Q3CanvasView(canvas,frame);
sarea
->setBackgroundRole
(QPalette::Dark);
resize(500, 400);
connect(mwin->actionOpen, SIGNAL(activated()), this, SLOT(open()));
connect(mwin->actionZoomIn, SIGNAL(activated()), this, SLOT(zoomIn()));
connect(mwin->actionZoomOut, SIGNAL(activated()), this, SLOT(zoomOut()));
connect(mwin->actionZoomNormal, SIGNAL(activated()), this, SLOT(zoomNormal()));
}
ImageZoomer::~ImageZoomer()
{
}
void ImageZoomer::open()
{
if (!filename.isEmpty())
{
if (image.isNull())
{
QMessageBox::information(this, tr
("Image Zoomer"),tr
("Cannot load %1.").
arg(filename
));
return;
}
canvas
->setBackgroundPixmap
(QPixmap::fromImage(image
));
scalefactor = 1.0;
}
}
void ImageZoomer::zoomIn()
{
scaleImage(1.25);
}
void ImageZoomer::zoomOut()
{
scaleImage(0.8);
}
void ImageZoomer::scaleImage(double factor)
{
int width,height;
Q_ASSERT(canvas->backgroundPixmap());
scalefactor *= factor;
width =(int) scalefactor * fromImage.width();
height =(int) scalefactor * fromImage.height();
canvas->resize(width,height);
adjustScrollBar(sarea->horizontalScrollBar(), factor);
adjustScrollBar(sarea->verticalScrollBar(), factor);
}
void ImageZoomer
::adjustScrollBar(QScrollBar *scrollBar,
double factor
) {
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
}
void ImageZoomer::fitToWindow()
{
scaleImage(1.0);
}
void ImageZoomer::zoomNormal()
{
bool zoomnormal = mwin->actionZoomNormal->isChecked();
sarea->setWidgetResizable(zoomnormal);
if (!zoomnormal) {
fitToWindow();
}
}
#include "imagezoomer.h"
#include <QtGui>
#include <qdir.h>
#include <QColor>
ImageZoomer::ImageZoomer(Ui::MainWindow *_mwin): mwin(_mwin)
{
frame = new QFrame(mwin->centralwidget);
frame->setGeometry(QRect(60, 70, 591, 571));
frame->setFrameShape(QFrame::StyledPanel);
frame->setFrameShadow(QFrame::Plain);
canvas = new Q3Canvas(frame);
//canview = new Q3CanvasView(frame);
//canview->setCanvas(canvas);
canvas->setBackgroundColor(Qt::black);
canvas->resize(400,400);
canview = new Q3CanvasView(canvas,frame);
sarea = new QScrollArea(frame);
sarea->setBackgroundRole(QPalette::Dark);
resize(500, 400);
connect(mwin->actionOpen, SIGNAL(activated()), this, SLOT(open()));
connect(mwin->actionZoomIn, SIGNAL(activated()), this, SLOT(zoomIn()));
connect(mwin->actionZoomOut, SIGNAL(activated()), this, SLOT(zoomOut()));
connect(mwin->actionZoomNormal, SIGNAL(activated()), this, SLOT(zoomNormal()));
}
ImageZoomer::~ImageZoomer()
{
}
void ImageZoomer::open()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open File"),QDir::currentPath());
if (!filename.isEmpty())
{
QImage image(filename);
if (image.isNull())
{
QMessageBox::information(this, tr("Image Zoomer"),tr("Cannot load %1.").arg(filename));
return;
}
canvas->setBackgroundPixmap(QPixmap::fromImage(image));
scalefactor = 1.0;
}
}
void ImageZoomer::zoomIn()
{
scaleImage(1.25);
}
void ImageZoomer::zoomOut()
{
scaleImage(0.8);
}
void ImageZoomer::scaleImage(double factor)
{
int width,height;
Q_ASSERT(canvas->backgroundPixmap());
scalefactor *= factor;
width =(int) scalefactor * fromImage.width();
height =(int) scalefactor * fromImage.height();
canvas->resize(width,height);
adjustScrollBar(sarea->horizontalScrollBar(), factor);
adjustScrollBar(sarea->verticalScrollBar(), factor);
}
void ImageZoomer::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
}
void ImageZoomer::fitToWindow()
{
scaleImage(1.0);
}
void ImageZoomer::zoomNormal()
{
bool zoomnormal = mwin->actionZoomNormal->isChecked();
sarea->setWidgetResizable(zoomnormal);
if (!zoomnormal) {
fitToWindow();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks