Seamly2D
Code documentation
vobjpaintdevice.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vobjpaintdevice.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 6 12, 2014
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2013-2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vobjpaintdevice.h"
53 
54 #include <QFile>
55 #include <QIODevice>
56 #include <QMessageLogger>
57 #include <QtDebug>
58 
59 #include "vobjengine.h"
60 
61 //---------------------------------------------------------------------------------------------------------------------
63  :QPaintDevice(), engine(new VObjEngine()), fileName(), owns_iodevice(1)
64 {
65  owns_iodevice = static_cast<int>(false);
66 }
67 
68 //---------------------------------------------------------------------------------------------------------------------
70 {
71  if (owns_iodevice)
72  {
73  delete engine->getOutputDevice();
74  }
75 }
76 
77 //---------------------------------------------------------------------------------------------------------------------
78 // cppcheck-suppress unusedFunction
79 QPaintEngine *VObjPaintDevice::paintEngine() const
80 {
81  return engine.data();
82 }
83 
84 //---------------------------------------------------------------------------------------------------------------------
85 // cppcheck-suppress unusedFunction
87 {
88  return fileName;
89 }
90 
91 //---------------------------------------------------------------------------------------------------------------------
92 void VObjPaintDevice::setFileName(const QString &value)
93 {
94  if (engine->isActive())
95  {
96  qWarning("VObjPaintDevice::setFileName(), cannot set file name while OBJ is being generated");
97  return;
98  }
99 
100  if (owns_iodevice)
101  {
102  delete engine->getOutputDevice();
103  }
104 
105  owns_iodevice = static_cast<int>(true);
106 
107  fileName = value;
108  QFile *file = new QFile(fileName);
109  engine->setOutputDevice(file);
110 }
111 
112 //---------------------------------------------------------------------------------------------------------------------
114 {
115  return engine->getSize();
116 }
117 
118 //---------------------------------------------------------------------------------------------------------------------
119 void VObjPaintDevice::setSize(const QSize &size)
120 {
121  if (engine->isActive())
122  {
123  qWarning("VObjPaintDevice::setSize(), cannot set size while OBJ is being generated");
124  return;
125  }
126  engine->setSize(size);
127 }
128 
129 //---------------------------------------------------------------------------------------------------------------------
131 {
132  return engine->getOutputDevice();
133 }
134 
135 //---------------------------------------------------------------------------------------------------------------------
136 void VObjPaintDevice::setOutputDevice(QIODevice *outputDevice)
137 {
138  if (engine->isActive())
139  {
140  qWarning("VObjPaintDevice::setOutputDevice(), cannot set output device while OBJ is being generated");
141  return;
142  }
143  owns_iodevice = static_cast<int>(false);
144  engine->setOutputDevice(outputDevice);
145  fileName = QString();
146 }
147 
148 //---------------------------------------------------------------------------------------------------------------------
150 {
151  return engine->getResolution();
152 }
153 
154 //---------------------------------------------------------------------------------------------------------------------
156 {
157  engine->setResolution(dpi);
158 }
159 
160 //---------------------------------------------------------------------------------------------------------------------
161 int VObjPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
162 {
163  switch (metric)
164  {
165  case QPaintDevice::PdmDepth:
166  return 32;
167  case QPaintDevice::PdmWidth:
168  return engine->getSize().width();
169  case QPaintDevice::PdmHeight:
170  return engine->getSize().height();
171  case QPaintDevice::PdmHeightMM:
172  return qRound(engine->getSize().height() * 25.4 / engine->getResolution());
173  case QPaintDevice::PdmWidthMM:
174  return qRound(engine->getSize().width() * 25.4 / engine->getResolution());
175  case QPaintDevice::PdmNumColors:
176  return static_cast<int>(0xffffffff);
177  case QPaintDevice::PdmDpiX:
178  case QPaintDevice::PdmDpiY:
179  case QPaintDevice::PdmPhysicalDpiX:
180  case QPaintDevice::PdmPhysicalDpiY:
181  return engine->getResolution();
182  case QPaintDevice::PdmDevicePixelRatio:
183  case QPaintDevice::PdmDevicePixelRatioScaled:
184  return 1;
185  default:
186  qWarning("VObjPaintDevice::metric(), unhandled metric %d\n", metric);
187  break;
188  }
189  return 0;
190 }
virtual ~VObjPaintDevice() Q_DECL_OVERRIDE
virtual QPaintEngine * paintEngine() const Q_DECL_OVERRIDE
QIODevice * getOutputDevice()
void setResolution(int dpi)
QString getFileName() const
void setSize(const QSize &size)
QSharedPointer< VObjEngine > engine
void setFileName(const QString &value)
virtual int metric(PaintDeviceMetric metric) const Q_DECL_OVERRIDE
void setOutputDevice(QIODevice *outputDevice)
int getResolution() const