Debian Codegraph @

src/graphicwindow.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #include "graphicwindow.h"
24
25 GraphicWindow::GraphicWindow(QWidget *parent) :
26 QWidget(parent)
27 {
28 qDebug("GraphicWindow::GraphicWindow");
29 imageLabel = new QLabel;
30 imageLabel->setBackgroundRole(QPalette::Base);
31 imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
32 imageLabel->setScaledContents(true);
33
34 m_toolbar = new QToolBar(this);
35 m_toolbar->setFloatable(false);
36
37 scrollArea = new QScrollArea(this);
38 scrollArea->setBackgroundRole(QPalette::Dark);
39 scrollArea->setWidget(imageLabel);
40 // setCentralWidget(scrollArea);
41
42 QVBoxLayout *m_layout = new QVBoxLayout(this);
43 m_layout->addWidget(m_toolbar);
44 m_layout->addWidget(scrollArea);
45 createActions();
46 // createMenus();
47
48 setWindowTitle(tr("Code Graphic Viewer"));
49 // show()
50
51 resize(800, 600);
52 setWindowFlags(Qt::Window);
53
54 // setWindowModality(Qt::WindowModal);
55 // m_scene = new QGraphicsScene(this);
56 //
57 // QGraphicsView view(m_scene, this);
58 // view.show();
59 }
60
61 GraphicWindow::~GraphicWindow()
62 {
63 qDebug("GraphicWindow::~GraphicWindow()");
64 // delete m_scene;
65 // QWidget::destroy();
66 }
67
68 void GraphicWindow::openPng(QString fileName)
69 {
70 qDebug() << "GraphicWindow::openPng" << fileName;
71 if (!fileName.isEmpty()) {
72 QImage image(fileName);
73 if (image.isNull()) {
74 QMessageBox::information(this, tr("Image Viewer"),
75 tr("Cannot load %1.").arg(fileName));
76 return;
77 }
78 imageLabel->setPixmap(QPixmap::fromImage(image));
79 scaleFactor = 1.0;
80
81 printAct->setEnabled(true);
82 saveAct->setEnabled(true);
83 fitToWindowAct->setEnabled(true);
84 updateActions();
85
86 if (!fitToWindowAct->isChecked())
87 imageLabel->adjustSize();
88 }
89 // fitToWindowAct->setChecked(true);
90 // fitToWindow();
91 }
92
93 void GraphicWindow::save()
94 {
95 QString fileName = QFileDialog::getSaveFileName(this,
96 tr("Save Image"), QString(), tr("Image Files (*.png *.jpg *.bmp)"));
97 if (fileName != "")
98 imageLabel->pixmap()->save(fileName);
99 }
100
101 void GraphicWindow::print()
102 {
103 Q_ASSERT(imageLabel->pixmap());
104 QPrintDialog dialog(&printer, this);
105 if (dialog.exec()) {
106 QPainter painter(&printer);
107 QRect rect = painter.viewport();
108 QSize size = imageLabel->pixmap()->size();
109 size.scale(rect.size(), Qt::KeepAspectRatio);
110 painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
111 painter.setWindow(imageLabel->pixmap()->rect());
112 painter.drawPixmap(0, 0, *imageLabel->pixmap());
113 }
114 }
115
116 void GraphicWindow::zoomIn()
117 {
118 scaleImage(1.25);
119 }
120
121 void GraphicWindow::zoomOut()
122 {
123 scaleImage(0.8);
124 }
125
126 void GraphicWindow::normalSize()
127 {
128 imageLabel->adjustSize();
129 scaleFactor = 1.0;
130 }
131
132 void GraphicWindow::fitToWindow()
133 {
134 bool fitToWindow = fitToWindowAct->isChecked();
135 scrollArea->setWidgetResizable(fitToWindow);
136 if (!fitToWindow) {
137 normalSize();
138 }
139 updateActions();
140 }
141
142 void GraphicWindow::scaleImage(double factor)
143 {
144 Q_ASSERT(imageLabel->pixmap());
145 scaleFactor *= factor;
146 imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
147
148 adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
149 adjustScrollBar(scrollArea->verticalScrollBar(), factor);
150
151 zoomInAct->setEnabled(scaleFactor < 3.0);
152 zoomOutAct->setEnabled(scaleFactor > 0.333);
153 }
154
155 void GraphicWindow::adjustScrollBar(QScrollBar *scrollBar, double factor)
156 {
157 scrollBar->setValue(int(factor * scrollBar->value()
158 + ((factor - 1) * scrollBar->pageStep()/2)));
159 }
160
161 void GraphicWindow::createActions()
162 {
163 // openAct = new QAction(tr("&Open..."), this);
164 // openAct->setShortcut(tr("Ctrl+O"));
165 // connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
166 saveAct = new QAction(tr("Save"), this);
167 saveAct->setShortcut(tr("Ctrl+S"));
168 saveAct->setEnabled(false);
169 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
170 m_toolbar->addAction(saveAct);
171
172 printAct = new QAction(tr("&Print..."), this);
173 printAct->setShortcut(tr("Ctrl+P"));
174 printAct->setEnabled(false);
175 connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
176 m_toolbar->addAction(printAct);
177
178 exitAct = new QAction(tr("E&xit"), this);
179 exitAct->setShortcut(tr("Ctrl+Q"));
180 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
181
182 zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
183 zoomInAct->setShortcut(tr("Ctrl++"));
184 zoomInAct->setEnabled(false);
185 connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
186 m_toolbar->addAction(zoomInAct);
187
188 zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
189 zoomOutAct->setShortcut(tr("Ctrl+-"));
190 zoomOutAct->setEnabled(false);
191 connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
192 m_toolbar->addAction(zoomOutAct);
193
194 normalSizeAct = new QAction(tr("&Normal Size"), this);
195 normalSizeAct->setShortcut(tr("Ctrl+S"));
196 normalSizeAct->setEnabled(false);
197 connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
198 m_toolbar->addAction(normalSizeAct);
199
200 fitToWindowAct = new QAction(tr("&Fit to Window"), this);
201 fitToWindowAct->setEnabled(false);
202 fitToWindowAct->setCheckable(true);
203 fitToWindowAct->setShortcut(tr("Ctrl+F"));
204 connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
205 m_toolbar->addAction(fitToWindowAct);
206
207 // aboutAct = new QAction(tr("&About"), this);
208 // connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
209 //
210 // aboutQtAct = new QAction(tr("About &Qt"), this);
211 // connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
212 }
213
214 // void GraphicWindow::createMenus()
215 // {
216 // fileMenu = new QMenu(tr("&File"), this);
217 // fileMenu->addAction(openAct);
218 // fileMenu->addAction(printAct);
219 // fileMenu->addSeparator();
220 // fileMenu->addAction(exitAct);
221 //
222 // viewMenu = new QMenu(tr("&View"), this);
223 // viewMenu->addAction(zoomInAct);
224 // viewMenu->addAction(zoomOutAct);
225 // viewMenu->addAction(normalSizeAct);
226 // viewMenu->addSeparator();
227 // viewMenu->addAction(fitToWindowAct);
228 //
229 // helpMenu = new QMenu(tr("&Help"), this);
230 // helpMenu->addAction(aboutAct);
231 // helpMenu->addAction(aboutQtAct);
232
233 // menuBar()->addMenu(fileMenu);
234 // menuBar()->addMenu(viewMenu);
235 // menuBar()->addMenu(helpMenu);
236 // }
237
238 void GraphicWindow::updateActions()
239 {
240 zoomInAct->setEnabled(!fitToWindowAct->isChecked());
241 zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
242 normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
243 }
244
245 // void GraphicWindow::closeEvent (QCloseEvent * event)
246 // {
247 // qDebug("GraphicWindow::closeEvent");
248 // // destroy();
249 // }