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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/10/2010 Created this file. :-)
17 #include "librarywidget.h"
20 #include "rs_staticgraphicview.h"
21 #include "rs_system.h"
22 #include "paintinterface.h"
24 LibraryWidget::LibraryWidget(QWidget * parent/*= 0*/, Qt::WindowFlags flags/*= 0*/):
25 QWidget(parent, flags), actionHandler(NULL)
29 QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
31 for(QStringList::Iterator it = directoryList.begin(); it!=directoryList.end(); ++it)
32 appendTree(NULL, (*it));
35 LibraryWidget::~LibraryWidget()
39 void LibraryWidget::setActionHandler(ActionHandler * ah)
45 * Escape releases focus.
47 void LibraryWidget::keyPressEvent(QKeyEvent * e)
56 QWidget::keyPressEvent(e);
64 void LibraryWidget::insert()
67 // Q3IconViewItem * item = ivPreview->currentItem();
68 QListWidgetItem * item = ivPreview->currentItem();
69 QString dxfPath = getItemPath(item);
71 if (QFileInfo(dxfPath).isReadable())
73 if (actionHandler != NULL)
75 ActionInterface * a = actionHandler->setCurrentAction(RS2::ActionLibraryInsert);
79 ActionLibraryInsert * action = (ActionLibraryInsert *)a;
80 action->setFile(dxfPath);
84 RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::insert:"
85 "Cannot create action ActionLibraryInsert");
91 RS_DEBUG->print(RS_Debug::D_ERROR,
92 "LibraryWidget::insert: Can't read file: '%s'", dxfPath.toLatin1().data());
95 #warning "!!! Need to port to Qt4 !!!"
100 * Appends the given directory to the given list view item. Called recursively until all
101 * library directories are appended.
103 void LibraryWidget::appendTree(ListViewItem * item, QString directory)
106 QStringList::Iterator it;
109 // read subdirectories of this directory:
112 QStringList lDirectoryList = dir.entryList(QDir::Dirs, QDir::Name);
114 ListViewItem* newItem;
115 ListViewItem* searchItem;
117 for(it=lDirectoryList.begin(); it!=lDirectoryList.end(); ++it)
119 if ((*it) != "." && (*it) != "..")
123 // Look for an item already existing and take this
124 // instead of making a new one:
126 searchItem = (ListViewItem *)item->firstChild();
128 searchItem = (ListViewItem *)lvDirectory->firstChild();
130 while (searchItem != NULL)
132 if (searchItem->text(0) == (*it))
138 searchItem = (ListViewItem *)searchItem->nextSibling();
141 // Create new item if no existing was found:
145 newItem = new ListViewItem(item, (*it));
147 newItem = new ListViewItem(lvDirectory, (*it));
150 appendTree(newItem, directory + "/" + (*it));
155 #warning "!!! Need to port to Qt4 !!!"
160 * Updates the icon preview.
162 //void LibraryWidget::updatePreview(Q3ListViewItem * item)
163 void LibraryWidget::updatePreview(QListWidgetItem * item)
169 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
171 // dir from the point of view of the library browser (e.g. /mechanical/screws)
172 QString directory = getItemDir(item);
175 // List of all directories that contain part libraries:
176 QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
177 QStringList::Iterator it;
179 QStringList itemPathList;
181 // look in all possible system directories for DXF files in the current library path:
182 for(it=directoryList.begin(); it!=directoryList.end(); ++it)
184 itemDir.setPath((*it) + directory);
186 if (itemDir.exists())
188 QStringList itemNameList = itemDir.entryList("*.dxf", QDir::Files, QDir::Name);
189 QStringList::Iterator it2;
191 for(it2=itemNameList.begin(); it2!=itemNameList.end(); ++it2)
192 itemPathList += itemDir.path() + "/" + (*it2);
199 // Fill items into icon view:
200 Q3IconViewItem * newItem;
201 for(it=itemPathList.begin(); it!=itemPathList.end(); ++it)
203 QString label = QFileInfo(*it).baseName(true);
204 QPixmap pixmap = getPixmap(directory, QFileInfo(*it).fileName(), (*it));
205 newItem = new Q3IconViewItem(ivPreview, label, pixmap);
208 QApplication::restoreOverrideCursor();
210 #warning "!!! Need to port to Qt4 !!!"
215 * @return Directory (in terms of the List view) to the given item (e.g. /mechanical/screws)
217 //QString LibraryWidget::getItemDir(Q3ListViewItem * item)
218 QString LibraryWidget::getItemDir(QListWidgetItem * item)
226 Q3ListViewItem* parent = item->parent();
227 return getItemDir(parent) + QString("/%1").arg(item->text(0));
229 #warning "!!! Need to port to Qt4 !!!"
235 * @return Path of the DXF file that is represented by the given item.
237 //QString LibraryWidget::getItemPath(Q3IconViewItem * item)
238 QString LibraryWidget::getItemPath(QListWidgetItem * item)
241 QString dir = getItemDir(lvDirectory->currentItem());
245 // List of all directories that contain part libraries:
246 QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
247 QStringList::Iterator it;
250 // look in all possible system directories for DXF files in the current library path:
251 for(it=directoryList.begin(); it!=directoryList.end(); ++it)
253 itemDir.setPath((*it)+dir);
255 if (itemDir.exists())
257 QString f = (*it) + dir + "/" + item->text() + ".dxf";
259 if (QFileInfo(f).isReadable())
271 #warning "!!! Need to port to Qt4 !!!"
277 * @return Pixmap that serves as icon for the given DXF File.
278 * The existing PNG file is returned or created and returned..
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)
285 QPixmap LibraryWidget::getPixmap(const QString & dir, const QString & dxfFile,
286 const QString & dxfPath)
288 QString pngFile = getPathToPixmap(dir, dxfFile, dxfPath);
289 QFileInfo fiPng(pngFile);
291 // found existing thumbnail:
293 return QPixmap(pngFile);
294 // default thumbnail:
296 return QPixmap(64, 64);
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.
303 QString LibraryWidget::getPathToPixmap(const QString & dir, const QString & dxfFile,
304 const QString & dxfPath)
306 RS_DEBUG->print("LibraryWidget::getPathToPixmap: dir: '%s' dxfFile: '%s' dxfPath: '%s'",
307 dir.toLatin1().data(), dxfFile.toLatin1().data(), dxfPath.toLatin1().data());
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;
314 QFileInfo fiDxf(dxfPath);
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)
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);
329 // the thumbnail exists:
332 RS_DEBUG->print("LibraryWidget::getPathToPixmap: dxf date: %s, png date: %s",
333 fiDxf.lastModified().toString().toLatin1().data(), fiPng.lastModified().toString().toLatin1().data());
335 if (fiPng.lastModified() > fiDxf.lastModified())
337 RS_DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail found: '%s'",
338 pngPath.toLatin1().data());
343 RS_DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail needs to be updated: '%s'",
344 pngPath.toLatin1().data());
349 // the thumbnail must be created in the user's home.
351 // create all directories needed:
352 RS_SYSTEM->createHomePath("/.qcad/library" + dir);
353 /*QString d = "/.qcad/library" + dir;
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);
361 if (created.isEmpty() || QFileInfo(created).isDir() || dr.mkdir(created, true)) {
362 RS_DEBUG->print("LibraryWidget: Created directory '%s'",
363 created.toLatin1().data());
366 RS_DEBUG->print(RS_Debug::D_ERROR,
367 "LibraryWidget: Cannot create directory '%s'",
368 created.toLatin1().data());
374 QString d = RS_SYSTEM->getHomeDir() + "/.qcad/library" + dir;
376 // pngPath = d + "/" + fiDxf.baseName(true) + ".png";
377 pngPath = d + "/" + fiDxf.completeBaseName() + ".png";
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);
389 RS_StaticGraphicView gv(128, 128, painter);
392 if (graphic.open(dxfPath, RS2::FormatUnknown))
394 RS_Color black(0, 0, 0);
396 for(RS_Entity * e=graphic.firstEntity(RS2::ResolveAll); e!=NULL; e=graphic.nextEntity(RS2::ResolveAll))
398 RS_Pen pen = e->getPen();
403 gv.setContainer(&graphic);
405 gv.drawEntity(&graphic, true);
407 #warning "Needs porting to Qt4... !!! FIX !!!"
412 img = img.smoothScale(64, 64);
414 iio.setFileName(pngPath);
415 iio.setFormat("PNG");
420 RS_DEBUG->print(RS_Debug::D_ERROR,
421 "LibraryWidget::getPathToPixmap: Cannot write thumbnail: '%s'",
422 pngPath.toLatin1().data());
428 RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::getPathToPixmap: Cannot open file: '%s'",
429 dxfPath.toLatin1().data());
432 // GraphicView deletes painter