Crossfire Server, Trunk  R20513
CRESmoothFaceMaker.cpp
Go to the documentation of this file.
1 #include "CRESmoothFaceMaker.h"
2 #include "CREPixmap.h"
3 #include <QtGui>
4 
5 extern "C" {
6 #include "global.h"
7 #include "image.h"
8 }
9 
11 {
12  QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
13 
14  myAutoClose = false;
15  myOverwrite = false;
16 
17  QGridLayout* layout = new QGridLayout(this);
18  int line = 0;
19 
20  layout->addWidget(new QLabel(tr("Face to use:"), this), line, 0);
21  myFace = new QComboBox(this);
22  layout->addWidget(myFace, line++, 1, 1, 2);
23  for (unsigned int face = 1; face < nrofpixmaps; face++)
24  myFace->addItem(CREPixmap::getIcon(face), new_faces[face].name, face);
25 
26  layout->addWidget(new QLabel(tr("Path of the picture to create:"), this), line, 0);
27  myDestination = new QLineEdit();
28  layout->addWidget(myDestination, line, 1);
29  connect(myDestination, SIGNAL(textEdited(const QString &)), this, SLOT(destinationEdited(const QString&)));
30 
31  QPushButton* browse = new QPushButton(tr("Browse"), this);
32  layout->addWidget(browse, line++, 2);
33  connect(browse, SIGNAL(clicked(bool)), this, SLOT(browse(bool)));
34 
35  QDialogButtonBox* box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close, Qt::Horizontal, this);
36  layout->addWidget(box, line++, 1, 1, 3);
37  connect(box, SIGNAL(rejected()), this, SLOT(reject()));
38  connect(box, SIGNAL(accepted()), this, SLOT(makeSmooth()));
39 
40  setWindowTitle(tr("Smooth face base generator"));
41 
42  QApplication::restoreOverrideCursor();
43 }
44 
46 {
47 }
48 
50 {
51  return myFace->itemData(myFace->currentIndex()).toInt();
52 }
53 
55 {
56  int line = myFace->findData(face);
57  if (line != -1)
58  myFace->setCurrentIndex(line);
59 }
60 
62 {
63  return myDestination->text();
64 }
65 
67 {
68  myAutoClose = autoClose;
69 }
70 
72 {
73  if (destination().isEmpty())
74  {
75  QMessageBox::warning(this, tr("Oops"), tr("You must select a destination!"));
76  return;
77  }
78 
79  if (!myOverwrite && QFile::exists(destination()))
80  {
81  if (QMessageBox::question(this, tr("Confirm file overwrite"), tr("File %1 already exists. Overwrite it?").arg(destination()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
82  return;
83  }
84 
85  QPixmap pic(32 * 16, 32 * 2);
86  pic.fill(QColor(0, 0, 0, 0));
87  QImage icon = CREPixmap::getIcon(selectedFace()).pixmap(32, 32).toImage();
88  QPainter device(&pic);
89  device.setBackground(QColor(0, 0, 0, 0));
90 
91  long spots[] = {
92  0x0, /* 0b000000000 */
93  0x49, /* 0b001001001 */
94  0x7, /* 0b000000111 */
95  0x4f, /* 0b001001111 */
96  0x124, /* 0b100100100 */
97  0x16d, /* 0b101101101 */
98  0x127, /* 0b100100111 */
99  0x16f, /* 0b101101111 */
100  0x1c0, /* 0b111000000 */
101  0x1c9, /* 0b111001001 */
102  0x1c7, /* 0b111000111 */
103  0x1cf, /* 0b111001111 */
104  0x1e4, /* 0b111100100 */
105  0x1ed, /* 0b111101101 */
106  0x1e7, /* 0b111100111 */
107  0x1ef, /* 0b111101111 */
108  0x0, /* 0b000000000 */
109  0x1, /* 0b000000001 */
110  0x4, /* 0b000000100 */
111  0x5, /* 0b000000101 */
112  0x100, /* 0b100000000 */
113  0x101, /* 0b100000001 */
114  0x104, /* 0b100000100 */
115  0x105, /* 0b100000101 */
116  0x40, /* 0b001000000 */
117  0x41, /* 0b001000001 */
118  0x44, /* 0b001000100 */
119  0x45, /* 0b001000101 */
120  0x140, /* 0b101000000 */
121  0x141, /* 0b101000001 */
122  0x144, /* 0b101000100 */
123  0x145, /* 0b101000101 */
124  };
125 
126 
127  int sx, sy, sw, sh, dx, dy, spot;
128  for (int x = 0; x < 16; x++) {
129  for (int y = 0; y < 2; y++) {
130  spot = y * 16 + x;
131 
132  for (int bit = 0; bit < 9; bit++) {
133  if ((spots[spot] & (1 << bit)) == 0)
134  continue;
135 
136  dx = x * 32;
137  dy = y * 32;
138  sw = 8;
139  sh = 8;
140 
141  switch (bit / 3)
142  {
143  case 0:
144  sy = 0;
145  break;
146  case 1:
147  sy = 8;
148  dy += 8;
149  sh = 16;
150  break;
151  case 2:
152  sy = 24;
153  dy += 24;
154  break;
155  default:
156  Q_ASSERT(false);
157  }
158 
159  switch (bit % 3)
160  {
161  case 0:
162  sx = 0;
163  break;
164  case 1:
165  sx = 8;
166  dx += 8;
167  sw = 16;
168  break;
169  case 2:
170  sx = 24;
171  dx += 24;
172  break;
173  default:
174  Q_ASSERT(false);
175  }
176 
177  device.drawImage(dx, dy, icon, sx, sy, sw, sh);
178  }
179 
180  }
181  }
182 
183  if (!pic.save(destination(), "PNG"))
184  {
185  QMessageBox::critical(this, tr("Error"), tr("Error while saving the picture as %1!").arg(destination()));
186  return;
187  }
188 
189  QMessageBox::information(this, tr("Smooth base saved"), tr("The smooth base was correctly saved as %1").arg(destination()));
190 
191  if (myAutoClose)
192  accept();
193 
194  myOverwrite = false;
195 }
196 
198 {
199  QString dest = QFileDialog::getSaveFileName(this, tr("Select destination file"), "", tr("PNG file (*.png);;All files (*.*)"));
200  if (dest.isEmpty())
201  return;
202 
203  myDestination->setText(dest);
204  myOverwrite = true;
205 }
206 
208 {
209  myOverwrite = false;
210 }
void destinationEdited(const QString &)
New_Face * new_faces
Contains face information, with names, numbers, magicmap color and such.
Definition: image.c:33
Global type definitions and header inclusions.
void setSelectedFace(int face)
unsigned int nrofpixmaps
Number of bitmaps loaded from the "bmaps" file.
Definition: image.c:45
const char * name
Face name, as used by archetypes and such.
Definition: face.h:20
Image-related structures.
static QIcon getIcon(int faceNumber)
Definition: CREPixmap.cpp:27
void setAutoClose(bool autoClose=true)
QString destination() const
Definition: main.c:88