]> Shamusworld >> Repos - architektonas/blobdiff - src/forms/librarywidget.cpp
Phase two of adding polyline functionality...
[architektonas] / src / forms / librarywidget.cpp
index 7657a5d8e0d1d9ca1cd59c1281653de607b59379..19a194dd1feff2c60931e27edad4fcf00e5b8485 100644 (file)
@@ -3,30 +3,58 @@
 // 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 <jlhamm@acm.org>
 //
 // 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
+// JLH  09/07/2010  Fully fixed thumbnail rendering
 //
 
+/*
+Note that this is basically just a way to get a block from a file; it's unclear
+that it does so and the doco (such as it was) didn't mention it either. So what
+we need is to make it very clear that inserting is to the BLOCK list and not
+the document--perhaps we need to fold it into the block list, and make the
+insert function insert into the block list only...
+Also, it would be useful to have some information like base unit, dimensions, etc.
+*/
+
 #include "librarywidget.h"
 
+#include "actionhandler.h"
+#include "actionlibraryinsert.h"
 #include "drawing.h"
-#include "rs_staticgraphicview.h"
-#include "rs_system.h"
+#include "staticgraphicview.h"
+#include "system.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");
+       QStringList directoryList = SYSTEM->getDirectoryList("library");
+#if 0
+std::cout << "directorySize = " << directoryList.size() << std::endl;
 
-       for(QStringList::Iterator it = directoryList.begin(); it!=directoryList.end(); ++it)
+for(int i=0; i<directoryList.size(); i++)
+       std::cout << directoryList.at(i).toLocal8Bit().constData() << std::endl;
+
+std::cout.flush();
+#endif
+
+       for(QStringList::Iterator it=directoryList.begin(); it!=directoryList.end(); it++)
                appendTree(NULL, (*it));
 }
 
@@ -34,7 +62,7 @@ LibraryWidget::~LibraryWidget()
 {
 }
 
-void LibraryWidget::setActionHandler(QG_ActionHandler * ah)
+void LibraryWidget::setActionHandler(ActionHandler * ah)
 {
        actionHandler = ah;
 }
@@ -57,133 +85,122 @@ void LibraryWidget::keyPressEvent(QKeyEvent * e)
 }
 
 /**
-* Insert.
-*/
+ * Insert.
+ */
 void LibraryWidget::insert()
 {
-#if 0
-//     Q3IconViewItem * item = ivPreview->currentItem();
-       QListWidgetItem * item = ivPreview->currentItem();
+       QListWidgetItem * item = ui.ivPreview->currentItem();
        QString dxfPath = getItemPath(item);
 
        if (QFileInfo(dxfPath).isReadable())
        {
-               if (actionHandler != NULL)
+               if (actionHandler)
                {
                        ActionInterface * a = actionHandler->setCurrentAction(RS2::ActionLibraryInsert);
 
-                       if (a != NULL)
+                       if (a)
                        {
                                ActionLibraryInsert * action = (ActionLibraryInsert *)a;
                                action->setFile(dxfPath);
                        }
                        else
                        {
-                               RS_DEBUG->print(RS_Debug::D_ERROR, "LibraryWidget::insert:"
+                               DEBUG->print(Debug::D_ERROR, "LibraryWidget::insert:"
                                        "Cannot create action ActionLibraryInsert");
                        }
                }
        }
        else
        {
-               RS_DEBUG->print(RS_Debug::D_ERROR,
+               DEBUG->print(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:
-                               if (item != NULL)
-                                       searchItem = (QG_ListViewItem *)item->firstChild();
-                               else
-                                       searchItem = (QG_ListViewItem *)lvDirectory->firstChild();
+                       // 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));
 
-                               while (searchItem != NULL)
+                       if (searchItem)
+                       {
+                               for(int i=0; i<searchItem->childCount(); 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));
                        }
+
+//This is picking up the directory tree TWICE, but ONLY if there are no thumbnails!
+//Actually, only if there is no corresponding thumbnail DIRECTORY under .architektonas...
+//Dunno why...
+//It was picking up .architektonas from the home directory in rs_system.cpp, that's why!
+//printf("LibraryWidget::appendTree: *it=\"%s\"\n", (*it).toAscii().data());
+                       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 directoryList = 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 +212,55 @@ 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));
+//printf("LibraryWidget: label = \"%s\"\n", label.toAscii().data());
        }
 
        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 directoryList = 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,55 +270,47 @@ 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/.architektonas/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)
 {
-       RS_DEBUG->print("LibraryWidget::getPathToPixmap: dir: '%s' dxfFile: '%s' dxfPath: '%s'",
+       DEBUG->print("LibraryWidget::getPathToPixmap: dir: '%s' dxfFile: '%s' dxfPath: '%s'",
                dir.toLatin1().data(), dxfFile.toLatin1().data(), dxfPath.toLatin1().data());
 
        // List of all directories that contain part libraries:
-       QStringList directoryList = RS_SYSTEM->getDirectoryList("library");
-       directoryList.prepend(RS_SYSTEM->getHomeDir() + "/.qcad/library");
+       QStringList directoryList = SYSTEM->getDirectoryList("library");
+       directoryList.prepend(SYSTEM->getHomeDir() + "/.architektonas/library");
        QStringList::Iterator it;
 
        QFileInfo fiDxf(dxfPath);
@@ -318,120 +322,82 @@ QString LibraryWidget::getPathToPixmap(const QString & dir, const QString & dxfF
        for(it=directoryList.begin(); it!=directoryList.end(); ++it)
        {
                itemDir = (*it) + dir;
-//             pngPath = itemDir + "/" + fiDxf.baseName(true) + ".png";
                pngPath = itemDir + "/" + fiDxf.completeBaseName() + ".png";
-               RS_DEBUG->print("LibraryWidget::getPathToPixmap: checking: '%s'",
+               DEBUG->print("LibraryWidget::getPathToPixmap: checking: '%s'",
                        pngPath.toLatin1().data());
                QFileInfo fiPng(pngPath);
 
                // the thumbnail exists:
                if (fiPng.isFile())
                {
-                       RS_DEBUG->print("LibraryWidget::getPathToPixmap: dxf date: %s, png date: %s",
+                       DEBUG->print("LibraryWidget::getPathToPixmap: dxf date: %s, png date: %s",
                                fiDxf.lastModified().toString().toLatin1().data(), fiPng.lastModified().toString().toLatin1().data());
 
                        if (fiPng.lastModified() > fiDxf.lastModified())
                        {
-                               RS_DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail found: '%s'",
+                               DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail found: '%s'",
                                        pngPath.toLatin1().data());
                                return pngPath;
                        }
                        else
                        {
-                               RS_DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail needs to be updated: '%s'",
+                               DEBUG->print("LibraryWidget::getPathToPixmap: thumbnail needs to be updated: '%s'",
                                        pngPath.toLatin1().data());
                        }
                }
        }
 
-       // 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;
-
-       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;
-
-//     pngPath = d + "/" + fiDxf.baseName(true) + ".png";
+       // The thumbnail must be created in the user's home.
+       // So, create all directories needed:
+       SYSTEM->createHomePath("/.architektonas/library" + dir);
+       QString d = SYSTEM->getHomeDir() + "/.architektonas/library" + dir;
        pngPath = d + "/" + fiDxf.completeBaseName() + ".png";
 
-       QPixmap * buffer = new QPixmap(128, 128);
-//     RS_PainterQt * painter = new RS_PainterQt(buffer);
-       QPainter qpntr(buffer);
+       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;
+       StaticGraphicView view(128, 128, painter);
+       Drawing drawing;
 
-       if (graphic.open(dxfPath, RS2::FormatUnknown))
+       if (drawing.open(dxfPath, RS2::FormatUnknown))
        {
-               RS_Color black(0, 0, 0);
+               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(Entity * e=drawing.firstEntity(RS2::ResolveAll); e!=NULL; e=drawing.nextEntity(RS2::ResolveAll))
                {
-                       RS_Pen pen = e->getPen();
+                       Pen pen = e->getPen();
                        pen.setColor(black);
                        e->setPen(pen);
                }
 
-               gv.setContainer(&graphic);
-               gv.zoomAuto(false);
-               gv.drawEntity(&graphic, true);
+               view.setContainer(&drawing);
+               view.zoomAuto(false);
+               view.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,
+                       DEBUG->print(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());
+               DEBUG->print(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;
 }