]> Shamusworld >> Repos - architektonas/blob - src/forms/librarywidget.cpp
GPL compliance check...
[architektonas] / src / forms / librarywidget.cpp
1 // librarywidget.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/10/2010  Created this file. :-)
15 //
16
17 #include "librarywidget.h"
18
19 #include "drawing.h"
20 #include "rs_staticgraphicview.h"
21 #include "rs_system.h"
22 #include "paintinterface.h"
23
24 LibraryWidget::LibraryWidget(QWidget * parent/*= 0*/, Qt::WindowFlags flags/*= 0*/):
25         QWidget(parent, flags), actionHandler(NULL)
26 {
27         ui.setupUi(this);
28
29         QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
30
31         for(QStringList::Iterator it = directoryList.begin(); it!=directoryList.end(); ++it)
32                 appendTree(NULL, (*it));
33 }
34
35 LibraryWidget::~LibraryWidget()
36 {
37 }
38
39 void LibraryWidget::setActionHandler(QG_ActionHandler * ah)
40 {
41         actionHandler = ah;
42 }
43
44 /**
45  * Escape releases focus.
46  */
47 void LibraryWidget::keyPressEvent(QKeyEvent * e)
48 {
49         switch (e->key())
50         {
51         case Qt::Key_Escape:
52                 emit escape();
53                 break;
54
55         default:
56                 QWidget::keyPressEvent(e);
57                 break;
58         }
59 }
60
61 /**
62 * Insert.
63 */
64 void LibraryWidget::insert()
65 {
66 #if 0
67 //      Q3IconViewItem * item = ivPreview->currentItem();
68         QListWidgetItem * item = ivPreview->currentItem();
69         QString dxfPath = getItemPath(item);
70
71         if (QFileInfo(dxfPath).isReadable())
72         {
73                 if (actionHandler != NULL)
74                 {
75                         ActionInterface * a = actionHandler->setCurrentAction(RS2::ActionLibraryInsert);
76
77                         if (a != NULL)
78                         {
79                                 ActionLibraryInsert * action = (ActionLibraryInsert *)a;
80                                 action->setFile(dxfPath);
81                         }
82                         else
83                         {
84                                 RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::insert:"
85                                         "Cannot create action ActionLibraryInsert");
86                         }
87                 }
88         }
89         else
90         {
91                 RS_DEBUG->print(RS_Debug::D_ERROR,
92                         "LibraryWidget::insert: Can't read file: '%s'", dxfPath.toLatin1().data());
93         }
94 #else
95 #warning "!!! Need to port to Qt4 !!!"
96 #endif
97 }
98
99 /**
100 * Appends the given directory to the given list view item. Called recursively until all
101 * library directories are appended.
102 */
103 void LibraryWidget::appendTree(QG_ListViewItem * item, QString directory)
104 {
105 #if 0
106         QStringList::Iterator it;
107         QDir dir(directory);
108
109         // read subdirectories of this directory:
110         if (dir.exists())
111         {
112                 QStringList lDirectoryList = dir.entryList(QDir::Dirs, QDir::Name);
113
114                 QG_ListViewItem* newItem;
115                 QG_ListViewItem* searchItem;
116
117                 for(it=lDirectoryList.begin(); it!=lDirectoryList.end(); ++it)
118                 {
119                         if ((*it) != "." && (*it) != "..")
120                         {
121                                 newItem = NULL;
122
123                                 // Look for an item already existing and take this
124                                 //   instead of making a new one:
125                                 if (item != NULL)
126                                         searchItem = (QG_ListViewItem *)item->firstChild();
127                                 else
128                                         searchItem = (QG_ListViewItem *)lvDirectory->firstChild();
129
130                                 while (searchItem != NULL)
131                                 {
132                                         if (searchItem->text(0) == (*it))
133                                         {
134                                                 newItem=searchItem;
135                                                 break;
136                                         }
137
138                                         searchItem = (QG_ListViewItem *)searchItem->nextSibling();
139                                 }
140
141                                 // Create new item if no existing was found:
142                                 if (newItem == NULL)
143                                 {
144                                         if (item)
145                                                 newItem = new QG_ListViewItem(item, (*it));
146                                         else
147                                                 newItem = new QG_ListViewItem(lvDirectory, (*it));
148                                 }
149
150                                 appendTree(newItem, directory + "/" + (*it));
151                         }
152                 }
153         }
154 #else
155 #warning "!!! Need to port to Qt4 !!!"
156 #endif
157 }
158
159 /**
160 * Updates the icon preview.
161 */
162 //void LibraryWidget::updatePreview(Q3ListViewItem * item)
163 void LibraryWidget::updatePreview(QListWidgetItem * item)
164 {
165 #if 0
166         if (item == NULL)
167                 return;
168
169         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
170
171         // dir from the point of view of the library browser (e.g. /mechanical/screws)
172         QString directory = getItemDir(item);
173         ivPreview->clear();
174
175         // List of all directories that contain part libraries:
176         QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
177         QStringList::Iterator it;
178         QDir itemDir;
179         QStringList itemPathList;
180
181         // look in all possible system directories for DXF files in the current library path:
182         for(it=directoryList.begin(); it!=directoryList.end(); ++it)
183         {
184                 itemDir.setPath((*it) + directory);
185
186                 if (itemDir.exists())
187                 {
188                         QStringList itemNameList = itemDir.entryList("*.dxf", QDir::Files, QDir::Name);
189                         QStringList::Iterator it2;
190
191                         for(it2=itemNameList.begin(); it2!=itemNameList.end(); ++it2)
192                                 itemPathList += itemDir.path() + "/" + (*it2);
193                 }
194         }
195
196         // Sort entries:
197         itemPathList.sort();
198
199         // Fill items into icon view:
200         Q3IconViewItem * newItem;
201         for(it=itemPathList.begin(); it!=itemPathList.end(); ++it)
202         {
203                 QString label = QFileInfo(*it).baseName(true);
204                 QPixmap pixmap = getPixmap(directory, QFileInfo(*it).fileName(), (*it));
205                 newItem = new Q3IconViewItem(ivPreview, label, pixmap);
206         }
207
208         QApplication::restoreOverrideCursor();
209 #else
210 #warning "!!! Need to port to Qt4 !!!"
211 #endif
212 }
213
214 /**
215 * @return Directory (in terms of the List view) to the given item (e.g. /mechanical/screws)
216 */
217 //QString LibraryWidget::getItemDir(Q3ListViewItem * item)
218 QString LibraryWidget::getItemDir(QListWidgetItem * item)
219 {
220 #if 0
221         QString ret = "";
222
223         if (item == NULL)
224                 return ret;
225
226         Q3ListViewItem* parent = item->parent();
227         return getItemDir(parent) + QString("/%1").arg(item->text(0));
228 #else
229 #warning "!!! Need to port to Qt4 !!!"
230         return "";
231 #endif
232 }
233
234 /**
235 * @return Path of the DXF file that is represented by the given item.
236 */
237 //QString LibraryWidget::getItemPath(Q3IconViewItem * item)
238 QString LibraryWidget::getItemPath(QListWidgetItem * item)
239 {
240 #if 0
241         QString dir = getItemDir(lvDirectory->currentItem());
242
243         if (item != NULL)
244         {
245                 // List of all directories that contain part libraries:
246                 QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
247                 QStringList::Iterator it;
248                 QDir itemDir;
249
250                 // look in all possible system directories for DXF files in the current library path:
251                 for(it=directoryList.begin(); it!=directoryList.end(); ++it)
252                 {
253                         itemDir.setPath((*it)+dir);
254
255                         if (itemDir.exists())
256                         {
257                                 QString f = (*it) + dir + "/" + item->text() + ".dxf";
258
259                                 if (QFileInfo(f).isReadable())
260                                         return f;
261                         }
262                 }
263
264                 return "";
265         }
266         else
267         {
268                 return "";
269         }
270 #else
271 #warning "!!! Need to port to Qt4 !!!"
272         return "";
273 #endif
274 }
275
276 /**
277 * @return Pixmap that serves as icon for the given DXF File.
278 * The existing PNG file is returned or created and returned..
279 *
280 * @param dir Library directory (e.g. "/mechanical/screws")
281 * @param dxfFile File name (e.g. "screw1.dxf")
282 * @param dxfPath Full path to the existing DXF file on disk
283 *                          (e.g. /home/tux/.qcad/library/mechanical/screws/screw1.dxf)
284 */
285 QPixmap LibraryWidget::getPixmap(const QString & dir, const QString & dxfFile,
286         const QString & dxfPath)
287 {
288         QString pngFile = getPathToPixmap(dir, dxfFile, dxfPath);
289         QFileInfo fiPng(pngFile);
290
291         // found existing thumbnail:
292         if (fiPng.isFile())
293                 return QPixmap(pngFile);
294         // default thumbnail:
295         else
296                 return QPixmap(64, 64);
297 }
298
299 /**
300 * @return Path to the thumbnail of the given DXF file. If no thumbnail exists, one is
301 * created in the user's home. If no thumbnail can be created, an empty string is returned.
302 */
303 QString LibraryWidget::getPathToPixmap(const QString & dir, const QString & dxfFile,
304         const QString & dxfPath)
305 {
306         RS_DEBUG->print("LibraryWidget::getPathToPixmap: dir: '%s' dxfFile: '%s' dxfPath: '%s'",
307                 dir.toLatin1().data(), dxfFile.toLatin1().data(), dxfPath.toLatin1().data());
308
309         // List of all directories that contain part libraries:
310         QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
311         directoryList.prepend(RS_SYSTEM->getHomeDir() + "/.qcad/library");
312         QStringList::Iterator it;
313
314         QFileInfo fiDxf(dxfPath);
315         QString itemDir;
316         QString pngPath;
317
318         // look in all possible system directories for PNG files
319         //  in the current library path:
320         for(it=directoryList.begin(); it!=directoryList.end(); ++it)
321         {
322                 itemDir = (*it) + dir;
323 //              pngPath = itemDir + "/" + fiDxf.baseName(true) + ".png";
324                 pngPath = itemDir + "/" + fiDxf.completeBaseName() + ".png";
325                 RS_DEBUG->print("LibraryWidget::getPathToPixmap: checking: '%s'",
326                         pngPath.toLatin1().data());
327                 QFileInfo fiPng(pngPath);
328
329                 // the thumbnail exists:
330                 if (fiPng.isFile())
331                 {
332                         RS_DEBUG->print("LibraryWidget::getPathToPixmap: dxf date: %s, png date: %s",
333                                 fiDxf.lastModified().toString().toLatin1().data(), fiPng.lastModified().toString().toLatin1().data());
334
335                         if (fiPng.lastModified() > fiDxf.lastModified())
336                         {
337                                 RS_DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail found: '%s'",
338                                         pngPath.toLatin1().data());
339                                 return pngPath;
340                         }
341                         else
342                         {
343                                 RS_DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail needs to be updated: '%s'",
344                                         pngPath.toLatin1().data());
345                         }
346                 }
347         }
348
349         // the thumbnail must be created in the user's home.
350
351         // create all directories needed:
352         RS_SYSTEM->createHomePath("/.qcad/library" + dir);
353         /*QString d = "/.qcad/library" + dir;
354         QDir dr;
355
356         QStringList dirs = QStringList::split('/', d, false);
357         QString created = RS_SYSTEM->getHomeDir();
358         for (it=dirs.begin(); it!=dirs.end(); ++it) {
359                 created += QString("/%1").arg(*it);
360
361                 if (created.isEmpty() || QFileInfo(created).isDir() || dr.mkdir(created, true)) {
362         RS_DEBUG->print("LibraryWidget: Created directory '%s'",
363         created.toLatin1().data());
364                 }
365                 else {
366         RS_DEBUG->print(RS_Debug::D_ERROR,
367         "LibraryWidget: Cannot create directory '%s'",
368         created.toLatin1().data());
369                         return "";
370                 }
371 }
372         */
373
374         QString d = RS_SYSTEM->getHomeDir() + "/.qcad/library" + dir;
375
376 //      pngPath = d + "/" + fiDxf.baseName(true) + ".png";
377         pngPath = d + "/" + fiDxf.completeBaseName() + ".png";
378
379         QPixmap * buffer = new QPixmap(128, 128);
380 //      RS_PainterQt * painter = new RS_PainterQt(buffer);
381         QPainter qpntr(buffer);
382         PaintInterface * painter = new PaintInterface(&qpntr);
383 //      painter->setBackgroundColor(RS_Color(255, 255, 255));
384 //      painter->eraseRect(0, 0, 128, 128);
385 //      qpntr.setBackgroundColor(RS_Color(255, 255, 255));
386         qpntr.setBackground(Qt::white);
387         qpntr.eraseRect(0, 0, 128, 128);
388
389         RS_StaticGraphicView gv(128, 128, painter);
390         Drawing graphic;
391
392         if (graphic.open(dxfPath, RS2::FormatUnknown))
393         {
394                 RS_Color black(0, 0, 0);
395
396                 for(RS_Entity * e=graphic.firstEntity(RS2::ResolveAll); e!=NULL; e=graphic.nextEntity(RS2::ResolveAll))
397                 {
398                         RS_Pen pen = e->getPen();
399                         pen.setColor(black);
400                         e->setPen(pen);
401                 }
402
403                 gv.setContainer(&graphic);
404                 gv.zoomAuto(false);
405                 gv.drawEntity(&graphic, true);
406
407 #warning "Needs porting to Qt4... !!! FIX !!!"
408 #if 0
409                 QImageIO iio;
410                 QImage img;
411                 img = *buffer;
412                 img = img.smoothScale(64, 64);
413                 iio.setImage(img);
414                 iio.setFileName(pngPath);
415                 iio.setFormat("PNG");
416
417                 if (!iio.write())
418                 {
419                         pngPath = "";
420                         RS_DEBUG->print(RS_Debug::D_ERROR,
421                                 "LibraryWidget::getPathToPixmap: Cannot write thumbnail: '%s'",
422                                 pngPath.toLatin1().data());
423                 }
424 #endif
425         }
426         else
427         {
428                 RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::getPathToPixmap: Cannot open file: '%s'",
429                         dxfPath.toLatin1().data());
430         }
431
432         // GraphicView deletes painter
433 //      painter->end();
434         // No, it doesn't
435         delete painter;
436         delete buffer;
437
438         return pngPath;
439 }