X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fforms%2Flibrarywidget.cpp;h=8ca7ca1b5f361d746ead6144ad3231f1f9cec3af;hb=af32602b9d8075080d5805143f1cd47fc10672e1;hp=77f2b78b2445e4ff81020750ba5d482b8e26d2c3;hpb=89e127aa3dbd74d3158e6dbe0ca1703420c04395;p=architektonas diff --git a/src/forms/librarywidget.cpp b/src/forms/librarywidget.cpp index 77f2b78..8ca7ca1 100644 --- a/src/forms/librarywidget.cpp +++ b/src/forms/librarywidget.cpp @@ -3,30 +3,54 @@ // Part of the Architektonas Project // Originally part of QCad Community Edition by Andrew Mustun // Extensively rewritten and refactored by James L. Hammons -// (C) 2010 Underground Software +// Portions copyright (C) 2001-2003 RibbonSoft +// Copyright (C) 2010 Underground Software +// See the README and GPLv2 files for licensing and warranty information // // JLH = James L. Hammons // // Who When What // --- ---------- ----------------------------------------------------------- // JLH 05/10/2010 Created this file. :-) +// JLH 08/28/2010 Restored functionality to library browser +// JLH 09/06/2010 Partially fixed thumbnail rendering // +/* +BUGS: + +- Picks up thumbnail directories twice--only once if thumbnail dir doesn't exist +*/ + #include "librarywidget.h" +#include "actionhandler.h" +#include "actionlibraryinsert.h" #include "drawing.h" #include "rs_staticgraphicview.h" #include "rs_system.h" -#include "paintintf.h" +#include "paintinterface.h" LibraryWidget::LibraryWidget(QWidget * parent/*= 0*/, Qt::WindowFlags flags/*= 0*/): QWidget(parent, flags), actionHandler(NULL) { +#if 0 +std::cout << "LibraryWidget::LibraryWidget()" << std::endl; +#endif ui.setupUi(this); + ui.lvDirectory->setColumnCount(1); QStringList directoryList = RS_SYSTEM->getDirectoryList("library"); +#if 0 +std::cout << "directorySize = " << directoryList.size() << std::endl; + +for(int i=0; icurrentItem(); - QListWidgetItem * item = ivPreview->currentItem(); + QListWidgetItem * item = ui.ivPreview->currentItem(); QString dxfPath = getItemPath(item); if (QFileInfo(dxfPath).isReadable()) { - if (actionHandler != NULL) + if (actionHandler) { - RS_ActionInterface * a = actionHandler->setCurrentAction(RS2::ActionLibraryInsert); + ActionInterface * a = actionHandler->setCurrentAction(RS2::ActionLibraryInsert); - if (a != NULL) + if (a) { - RS_ActionLibraryInsert * action = (RS_ActionLibraryInsert *)a; + ActionLibraryInsert * action = (ActionLibraryInsert *)a; action->setFile(dxfPath); } else { RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::insert:" - "Cannot create action RS_ActionLibraryInsert"); + "Cannot create action ActionLibraryInsert"); } } } @@ -89,101 +111,87 @@ void LibraryWidget::insert() RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::insert: Can't read file: '%s'", dxfPath.toLatin1().data()); } -#else -#warning "!!! Need to port to Qt4 !!!" -#endif } /** -* Appends the given directory to the given list view item. Called recursively until all -* library directories are appended. -*/ -void LibraryWidget::appendTree(QG_ListViewItem * item, QString directory) + * Appends the given directory to the given list view item. Called recursively until all + * library directories are appended. + */ +void LibraryWidget::appendTree(QTreeWidgetItem * item, QString directory) { -#if 0 - QStringList::Iterator it; QDir dir(directory); - // read subdirectories of this directory: - if (dir.exists()) - { - QStringList lDirectoryList = dir.entryList(QDir::Dirs, QDir::Name); + if (!dir.exists()) + return; - QG_ListViewItem* newItem; - QG_ListViewItem* searchItem; + // read subdirectories of this directory: + QStringList lDirectoryList = dir.entryList(QDir::Dirs, QDir::Name); - for(it=lDirectoryList.begin(); it!=lDirectoryList.end(); ++it) + for(QStringList::Iterator it=lDirectoryList.begin(); it!=lDirectoryList.end(); it++) + { + if ((*it) != "." && (*it) != "..") { - if ((*it) != "." && (*it) != "..") - { - newItem = NULL; + // Look for an item already existing and take this instead of + // making a new one: + QTreeWidgetItem * newItem = NULL; + QTreeWidgetItem * searchItem = + (item ? item->child(0) : ui.lvDirectory->topLevelItem(0)); - // Look for an item already existing and take this - // instead of making a new one: - if (item != NULL) - searchItem = (QG_ListViewItem *)item->firstChild(); - else - searchItem = (QG_ListViewItem *)lvDirectory->firstChild(); - - while (searchItem != NULL) + if (searchItem) + { + for(int i=0; ichildCount(); i++) { - if (searchItem->text(0) == (*it)) + if (searchItem->child(i)->text(0) == (*it)) { - newItem=searchItem; + newItem = searchItem->child(i); break; } - - searchItem = (QG_ListViewItem *)searchItem->nextSibling(); - } - - // Create new item if no existing was found: - if (newItem == NULL) - { - if (item) - newItem = new QG_ListViewItem(item, (*it)); - else - newItem = new QG_ListViewItem(lvDirectory, (*it)); } + } - appendTree(newItem, directory + "/" + (*it)); + // Create new item if no existing was found: + if (!newItem) + { + QStringList list; + list << (*it); + newItem = (item ? new QTreeWidgetItem(item, list) : new QTreeWidgetItem(ui.lvDirectory, list)); } + + appendTree(newItem, directory + "/" + (*it)); } } -#else -#warning "!!! Need to port to Qt4 !!!" -#endif } /** -* Updates the icon preview. -*/ -//void LibraryWidget::updatePreview(Q3ListViewItem * item) -void LibraryWidget::updatePreview(QListWidgetItem * item) + * Updates the icon preview. + */ +void LibraryWidget::updatePreview(QTreeWidgetItem * item, int /*column*/) { -#if 0 - if (item == NULL) + if (!item) return; QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); // dir from the point of view of the library browser (e.g. /mechanical/screws) QString directory = getItemDir(item); - ivPreview->clear(); + ui.ivPreview->clear(); // List of all directories that contain part libraries: QStringList directoryList = RS_SYSTEM->getDirectoryList("library"); QStringList::Iterator it; QDir itemDir; QStringList itemPathList; + QStringList filter; + filter << "*.dxf"; - // look in all possible system directories for DXF files in the current library path: + // Look in all possible system directories for DXF files in the current library path: for(it=directoryList.begin(); it!=directoryList.end(); ++it) { itemDir.setPath((*it) + directory); if (itemDir.exists()) { - QStringList itemNameList = itemDir.entryList("*.dxf", QDir::Files, QDir::Name); + QStringList itemNameList = itemDir.entryList(filter, QDir::Files, QDir::Name); QStringList::Iterator it2; for(it2=itemNameList.begin(); it2!=itemNameList.end(); ++it2) @@ -195,60 +203,54 @@ void LibraryWidget::updatePreview(QListWidgetItem * item) itemPathList.sort(); // Fill items into icon view: - Q3IconViewItem * newItem; + QListWidgetItem * newItem; + for(it=itemPathList.begin(); it!=itemPathList.end(); ++it) { - QString label = QFileInfo(*it).baseName(true); +// QString label = QFileInfo(*it).baseName(true); + QString label = QFileInfo(*it).completeBaseName(); QPixmap pixmap = getPixmap(directory, QFileInfo(*it).fileName(), (*it)); - newItem = new Q3IconViewItem(ivPreview, label, pixmap); +// newItem = new QListWidgetItem(ui.ivPreview, label, pixmap); + newItem = new QListWidgetItem(QIcon(pixmap), label, ui.ivPreview); +//Doesn't do what we want... +// newItem->setSizeHint(QSize(64, 64)); } QApplication::restoreOverrideCursor(); -#else -#warning "!!! Need to port to Qt4 !!!" -#endif } /** -* @return Directory (in terms of the List view) to the given item (e.g. /mechanical/screws) -*/ -//QString LibraryWidget::getItemDir(Q3ListViewItem * item) -QString LibraryWidget::getItemDir(QListWidgetItem * item) + * @return Directory (in terms of the List view) to the given item (e.g. /mechanical/screws) + * (called recursively) + */ +QString LibraryWidget::getItemDir(QTreeWidgetItem * item) { -#if 0 - QString ret = ""; + if (!item) + return ""; - if (item == NULL) - return ret; + QTreeWidgetItem * parent = item->parent(); - Q3ListViewItem* parent = item->parent(); return getItemDir(parent) + QString("/%1").arg(item->text(0)); -#else -#warning "!!! Need to port to Qt4 !!!" - return ""; -#endif } /** -* @return Path of the DXF file that is represented by the given item. -*/ -//QString LibraryWidget::getItemPath(Q3IconViewItem * item) + * @return Path of the DXF file that is represented by the given item. + */ QString LibraryWidget::getItemPath(QListWidgetItem * item) { -#if 0 - QString dir = getItemDir(lvDirectory->currentItem()); + QString dir = getItemDir(ui.lvDirectory->currentItem()); - if (item != NULL) + if (item) { // List of all directories that contain part libraries: QStringList directoryList = RS_SYSTEM->getDirectoryList("library"); QStringList::Iterator it; QDir itemDir; - // look in all possible system directories for DXF files in the current library path: + // Look in all possible system directories for DXF files in the current library path: for(it=directoryList.begin(); it!=directoryList.end(); ++it) { - itemDir.setPath((*it)+dir); + itemDir.setPath((*it) + dir); if (itemDir.exists()) { @@ -258,46 +260,38 @@ QString LibraryWidget::getItemPath(QListWidgetItem * item) return f; } } - - return ""; - } - else - { - return ""; } -#else -#warning "!!! Need to port to Qt4 !!!" + return ""; -#endif } /** -* @return Pixmap that serves as icon for the given DXF File. -* The existing PNG file is returned or created and returned.. -* -* @param dir Library directory (e.g. "/mechanical/screws") -* @param dxfFile File name (e.g. "screw1.dxf") -* @param dxfPath Full path to the existing DXF file on disk -* (e.g. /home/tux/.qcad/library/mechanical/screws/screw1.dxf) -*/ + * @return Pixmap that serves as icon for the given DXF File. + * The existing PNG file is returned or created and returned.. + * + * @param dir Library directory (e.g. "/mechanical/screws") + * @param dxfFile File name (e.g. "screw1.dxf") + * @param dxfPath Full path to the existing DXF file on disk + * (e.g. /home/tux/.qcad/library/mechanical/screws/screw1.dxf) + */ QPixmap LibraryWidget::getPixmap(const QString & dir, const QString & dxfFile, const QString & dxfPath) { QString pngFile = getPathToPixmap(dir, dxfFile, dxfPath); QFileInfo fiPng(pngFile); - // found existing thumbnail: + // Found existing thumbnail: if (fiPng.isFile()) return QPixmap(pngFile); - // default thumbnail: + // Default thumbnail: else return QPixmap(64, 64); } /** -* @return Path to the thumbnail of the given DXF file. If no thumbnail exists, one is -* created in the user's home. If no thumbnail can be created, an empty string is returned. -*/ + * @return Path to the thumbnail of the given DXF file. If no thumbnail exists, one is + * created in the user's home. If no thumbnail can be created, an empty string is returned. + */ QString LibraryWidget::getPathToPixmap(const QString & dir, const QString & dxfFile, const QString & dxfPath) { @@ -306,7 +300,7 @@ QString LibraryWidget::getPathToPixmap(const QString & dir, const QString & dxfF // List of all directories that contain part libraries: QStringList directoryList = RS_SYSTEM->getDirectoryList("library"); - directoryList.prepend(RS_SYSTEM->getHomeDir() + "/.qcad/library"); + directoryList.prepend(RS_SYSTEM->getHomeDir() + "/.architektonas/library"); QStringList::Iterator it; QFileInfo fiDxf(dxfPath); @@ -344,94 +338,94 @@ QString LibraryWidget::getPathToPixmap(const QString & dir, const QString & dxfF } } - // the thumbnail must be created in the user's home. - - // create all directories needed: - RS_SYSTEM->createHomePath("/.qcad/library" + dir); - /*QString d = "/.qcad/library" + dir; - QDir dr; + // The thumbnail must be created in the user's home. + // So, create all directories needed: + RS_SYSTEM->createHomePath("/.architektonas/library" + dir); - QStringList dirs = QStringList::split('/', d, false); - QString created = RS_SYSTEM->getHomeDir(); - for (it=dirs.begin(); it!=dirs.end(); ++it) { - created += QString("/%1").arg(*it); - - if (created.isEmpty() || QFileInfo(created).isDir() || dr.mkdir(created, true)) { - RS_DEBUG->print("LibraryWidget: Created directory '%s'", - created.toLatin1().data()); - } - else { - RS_DEBUG->print(RS_Debug::D_ERROR, - "LibraryWidget: Cannot create directory '%s'", - created.toLatin1().data()); - return ""; - } -} - */ - - QString d = RS_SYSTEM->getHomeDir() + "/.qcad/library" + dir; + QString d = RS_SYSTEM->getHomeDir() + "/.architektonas/library" + dir; +//printf("librarywidget: d = \"%s\"\n", d.toAscii().data()); // pngPath = d + "/" + fiDxf.baseName(true) + ".png"; pngPath = d + "/" + fiDxf.completeBaseName() + ".png"; +//printf("librarywidget: pngPath = \"%s\"\n", pngPath.toAscii().data()); - QPixmap * buffer = new QPixmap(128, 128); -// RS_PainterQt * painter = new RS_PainterQt(buffer); - QPainter qpntr(buffer); + // Ugh. This is a mess and doesn't work right anyway... + // Problem is StaticGraphicView doesn't follow a standard rendering path, + // so all this crap just ends in failure. Not sure how to fix. +/** +I suppose one way to do it would be to make a CreateImage() function in GraphicView, +then we wouldn't have all this messiness... +I think the way the redraw works is like that--it creates its own PaintInterface +and QPainter and sets "painter" equal to it... That being the case, it would be +a simple matter to modify StaticGraphicView to utilize that... Let's see... +Which it should be doing, but, for some reason, it fails. Dunno why. + +It only fails for certain DXFs. Dunno why. +*/ + + QPixmap buffer(128, 128); + QPainter qpntr(&buffer); PaintInterface * painter = new PaintInterface(&qpntr); -// painter->setBackgroundColor(RS_Color(255, 255, 255)); -// painter->eraseRect(0, 0, 128, 128); -// qpntr.setBackgroundColor(RS_Color(255, 255, 255)); qpntr.setBackground(Qt::white); qpntr.eraseRect(0, 0, 128, 128); RS_StaticGraphicView gv(128, 128, painter); - Drawing graphic; + Drawing drawing; - if (graphic.open(dxfPath, RS2::FormatUnknown)) + if (drawing.open(dxfPath, RS2::FormatUnknown)) { RS_Color black(0, 0, 0); - for(RS_Entity * e=graphic.firstEntity(RS2::ResolveAll); e!=NULL; e=graphic.nextEntity(RS2::ResolveAll)) + // Set all pens in the drawing to BLACK + for(RS_Entity * e=drawing.firstEntity(RS2::ResolveAll); e!=NULL; e=drawing.nextEntity(RS2::ResolveAll)) { RS_Pen pen = e->getPen(); pen.setColor(black); e->setPen(pen); } - gv.setContainer(&graphic); + gv.setContainer(&drawing); gv.zoomAuto(false); - gv.drawEntity(&graphic, true); +//This works, but somehow doesn't work ALL the time + gv.drawEntity(&drawing, true); -#warning "Needs porting to Qt4... !!! FIX !!!" -#if 0 - QImageIO iio; - QImage img; - img = *buffer; - img = img.smoothScale(64, 64); - iio.setImage(img); - iio.setFileName(pngPath); - iio.setFormat("PNG"); - - if (!iio.write()) + QImageWriter writer; + QImage image = buffer.toImage(); + image.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + writer.setFileName(pngPath); + writer.setFormat("png"); + + if (!writer.write(image)) { - pngPath = ""; RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::getPathToPixmap: Cannot write thumbnail: '%s'", pngPath.toLatin1().data()); + pngPath = ""; } -#endif } else { - RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::getPathToPixmap: Cannot open file: '%s'", - dxfPath.toLatin1().data()); + RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::getPathToPixmap: Cannot open file: '%s'", dxfPath.toLatin1().data()); } - // GraphicView deletes painter -// painter->end(); - // No, it doesn't delete painter; - delete buffer; return pngPath; } + +#if 0 + +QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible +QPaintDevice: Cannot destroy paint device that is being painted + +Program received signal SIGSEGV, Segmentation fault. +0x085cd068 in ?? () +(gdb) bt +#0 0x085cd068 in ?? () +#1 0xb7818bd0 in QPainter::~QPainter() () from /usr/lib/qt4/libQtGui.so.4 +#2 0x081c1e8e in LibraryWidget::getPathToPixmap(QString const&, QString const&, QString const&) () +#3 0x081c0fa6 in LibraryWidget::getPixmap(QString const&, QString const&, QString const&) () +#4 0x081c0775 in LibraryWidget::updatePreview(QTreeWidgetItem*, int) () +#5 0x08237d5e in LibraryWidget::qt_metacall(QMetaObject::Call, int, void**) () + +#endif