From: Shamus Hammons Date: Sat, 26 Mar 2016 04:28:25 +0000 (-0500) Subject: Added preview window to file loading dialog. :-) X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=ttedit;a=commitdiff_plain Added preview window to file loading dialog. :-) --- diff --git a/src/charwindow.cpp b/src/charwindow.cpp index 76112e4..06b273d 100644 --- a/src/charwindow.cpp +++ b/src/charwindow.cpp @@ -24,16 +24,17 @@ #include "charwindow.h" #include "debug.h" -//CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Tool), path(NULL) + CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Window), path(NULL) { setWindowTitle("Character: Unknown"); } -void CharWindow::MakePathFromPoints(GlyphPoints * gp) + +QPainterPath * CharWindow::MakePathFromPoints(GlyphPoints * gp) { if (gp == NULL) - return; + return NULL; if (path != NULL) delete path; @@ -49,10 +50,10 @@ void CharWindow::MakePathFromPoints(GlyphPoints * gp) if (gp->GetNumPoints(poly) < 3) continue; - // Initial move: If our start point is on curve, then go to it. Otherwise, - // check previous point. If it's on curve, go to it otherwise go the - // midpoint between start point and previous (since it's between two curve - // control points). + // Initial move: If our start point is on curve, then go to it. + // Otherwise, check previous point. If it's on curve, go to it + // otherwise go the midpoint between start point and previous (since + // it's between two curve control points). IPoint pt = (gp->GetOnCurve(poly, 0) ? gp->GetPoint(poly, 0) : (gp->GetPrevOnCurve(poly, 0) ? gp->GetPrevPoint(poly, 0) : gp->GetMidpointToPrev(poly, 0))); @@ -70,10 +71,10 @@ void CharWindow::MakePathFromPoints(GlyphPoints * gp) if (gp->GetOnCurve(poly, i)) continue; - // We are now guaranteed that we are sitting on a curve control point - // (off curve). Figure the extent of the curve: If the following is a - // curve control point, then use the midpoint to it otherwise go to - // the next point since it's on curve. + // We are now guaranteed that we are sitting on a curve control + // point (off curve). Figure the extent of the curve: If the + // following is a curve control point, then use the midpoint to + // it otherwise go to the next point since it's on curve. IPoint pt = (gp->GetNextOnCurve(poly, i) ? gp->GetNextPoint(poly, i) : gp->GetMidpointToNext(poly, i)); @@ -83,18 +84,66 @@ void CharWindow::MakePathFromPoints(GlyphPoints * gp) path->closeSubpath(); } + + return path; } + +void CharWindow::RenderPathInImage(QPainterPath * path, QImage * img) +{ + if ((path == NULL) || (img == NULL)) + return; + + QPainter p(img); + + p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine)); + p.setBrush(Qt::black); + + QRectF rect = path->boundingRect(); + QSize paintSize = img->size(); + +#if 0 + // For some reason, this code cuts off two pixels when rendering the path. + // Not sure why, but we compensate for that here. + paintSize.rwidth() -= 2; + paintSize.rheight() -= 2; +#endif + + p.translate(0, paintSize.height()); + float extraX = 0.0f, extraY = 0.0f; + float xConvFac = (float)paintSize.width() / rect.width(); + float yConvFac = (float)paintSize.height() / rect.height(); + + if (xConvFac > yConvFac) + { + // height is limiting factor (smaller than width) + p.scale(yConvFac, -yConvFac); + extraX = (((float)paintSize.width() / yConvFac) - rect.width()) / 2.0f; + } + else + { + // width is limiting factor (smaller than height) + p.scale(xConvFac, -xConvFac); + extraY = (((float)paintSize.height() / xConvFac) - rect.height()) / 2.0f; + } + + p.translate(-rect.x() + extraX, -rect.y() + extraY); + p.drawPath(*path); +} + + QSize CharWindow::minimumSizeHint() const { return QSize(50, 50); } + QSize CharWindow::sizeHint() const { return QSize(200, 200); } + void CharWindow::paintEvent(QPaintEvent * /*event*/) { if (path == NULL) @@ -129,8 +178,6 @@ void CharWindow::paintEvent(QPaintEvent * /*event*/) { // width is limiting factor (smaller than height) p.scale(xConvFac, -xConvFac); -//extraY = (rect.width() / (float)paintSize.width()) * (float)paintSize.height(); -//extraY = (extraY - rect.height()) / 2.0f; extraY = (((float)paintSize.height() / xConvFac) - rect.height()) / 2.0f; } @@ -138,3 +185,4 @@ void CharWindow::paintEvent(QPaintEvent * /*event*/) p.drawPath(*path); } + diff --git a/src/charwindow.h b/src/charwindow.h index a5bfb18..410475f 100644 --- a/src/charwindow.h +++ b/src/charwindow.h @@ -17,40 +17,18 @@ class CharWindow: public QWidget public: CharWindow(QWidget * parent = NULL); - void MakePathFromPoints(GlyphPoints * gp); + QPainterPath * MakePathFromPoints(GlyphPoints * gp); + void RenderPathInImage(QPainterPath *, QImage *); protected: QSize minimumSizeHint() const; QSize sizeHint() const; void paintEvent(QPaintEvent * event); - private: + public: QImage img; QPainterPath * path; }; -#if 0 -#include // So that whoever uses this can without having - // to pull in a bunch of references manually -#include - -class CharWindow: public wxMiniFrame -{ - private: - protected: - public: - wxBitmap * bmp; - - // Constructor and destructor -// CharWindow(wxFrame * parent, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize, long style = 0); - CharWindow(wxFrame * parent, const wxString &title, const wxPoint &pos, const wxSize &size, long style); - ~CharWindow(void); - - void OnPaint(wxPaintEvent &e); -// void OnMouseEvent(wxMouseEvent &e); - - DECLARE_EVENT_TABLE() -}; -#endif - #endif // __CHARWINDOW_H__ + diff --git a/src/editwindow.cpp b/src/editwindow.cpp index d24c447..dac63c2 100644 --- a/src/editwindow.cpp +++ b/src/editwindow.cpp @@ -38,6 +38,7 @@ #include "debug.h" #include "global.h" #include "mainwindow.h" +#include "mathconstants.h" #include "ttedit.h" #include "vector.h" @@ -581,8 +582,9 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event) } else { - // Figure out the angle between the "zero" vector and the current one, - // then rotate all points relative to the "zero" vector (done by paint()) + // Figure out the angle between the "zero" vector and the + // current one, then rotate all points relative to the + // "zero" vector (done by paint()) // QPoint currentPoint = GetAdjustedMousePosition(event); Vector currentPoint = Painter::QtToCartesianCoords(Vector(event->x(), event->y())); Vector v1(rotationZeroPoint.x, rotationZeroPoint.y, 0, @@ -593,8 +595,8 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event) rotationAngle = v2.Angle(v1); QString s; - s.sprintf("%.3f degrees", rotationAngle * 180.0 / 3.14159265358979323); - ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage(s); + s.sprintf("%.3f degrees", rotationAngle * RADIANS_TO_DEGREES); + Global::mainWindow->statusBar()->showMessage(s); } update(); @@ -776,14 +778,14 @@ void EditWindow::mouseReleaseEvent(QMouseEvent * event) } update(); - ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage(""); + Global::mainWindow->statusBar()->showMessage(""); } // if (tool == TOOLScroll || tool == TOOLZoom) // ReleaseMouse(); //this is prolly too much - ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&pts); - ((TTEdit *)qApp)->charWnd->update(); + Global::charWnd->MakePathFromPoints(&pts); + Global::charWnd->update(); if (tool == TOOLMultiSelect) { @@ -849,8 +851,8 @@ void EditWindow::keyPressEvent(QKeyEvent * event) //Not need but you need to call the base class for some reason?? // event->accept(); update(); - ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&pts); - ((TTEdit *)qApp)->charWnd->update(); + Global::charWnd->MakePathFromPoints(&pts); + Global::charWnd->update(); } diff --git a/src/global.cpp b/src/global.cpp index ad562ee..f34229c 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -49,3 +49,6 @@ int Global::numLayers = 1; std::vector Global::layerHidden; std::vector Global::layerLocked; +CharWindow * Global::charWnd = 0; +MainWindow * Global::mainWindow = 0; + diff --git a/src/global.h b/src/global.h index 64422da..fc17a5a 100644 --- a/src/global.h +++ b/src/global.h @@ -10,6 +10,8 @@ #include "vector.h" class QFont; +class CharWindow; +class MainWindow; enum LineStyle { LSNone, LSSolid, LSDash, LSDot, LSDashDot, LSDashDotDot }; @@ -53,6 +55,9 @@ class Global static int numLayers; static std::vector layerHidden; static std::vector layerLocked; + + static CharWindow * charWnd; + static MainWindow * mainWindow; }; #endif // __GLOBALS_H__ diff --git a/src/glyphpoints.cpp b/src/glyphpoints.cpp index 856b859..025f0e1 100644 --- a/src/glyphpoints.cpp +++ b/src/glyphpoints.cpp @@ -907,24 +907,29 @@ bool GlyphPoints::LoadGlyphFromFile(FILE * file) FreeAllocatedMemory(); - fscanf(file, "%s V%f", line, &version); - fscanf(file, "%s %u", line, &numPoints); + int num = fscanf(file, "TTEGLYPH V%f\n", &version); + + // Check to see if this is really a glyph file + if ((num != 1) || (version != 1.0)) + return false; + + num = fscanf(file, "POINTS %u\n", &numPoints); x = new int[numPoints]; y = new int[numPoints]; onCurve = new bool[numPoints]; for(int i=0; icharWnd = new CharWindow(this); + Global::charWnd = new CharWindow(this); editWnd = new EditWindow(this); setCentralWidget(editWnd); editWnd->setFocus(); setWindowIcon(QIcon(":/res/ttedit.png")); setWindowTitle("TTEdit! - Untitled"); -#if 0 -// createActions(); - newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); - newAct->setShortcuts(QKeySequence::New); - newAct->setStatusTip(tr("Create a new file")); - connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); - - openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); - openAct->setShortcuts(QKeySequence::Open); - openAct->setStatusTip(tr("Open an existing file")); - connect(openAct, SIGNAL(triggered()), this, SLOT(open())); - - aboutQtAct = new QAction(tr("About &Qt"), this); - aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); - -// createMenus(); - fileMenu = menuBar()->addMenu(tr("&File")); - fileMenu->addAction(newAct); - fileMenu->addAction(openAct); - fileMenu->addAction(saveAct); - fileMenu->addAction(saveAsAct); - fileMenu->addSeparator(); - fileMenu->addAction(exitAct); - - editMenu = menuBar()->addMenu(tr("&Edit")); - editMenu->addAction(cutAct); - editMenu->addAction(copyAct); - editMenu->addAction(pasteAct); - - menuBar()->addSeparator(); - - helpMenu = menuBar()->addMenu(tr("&Help")); - helpMenu->addAction(aboutAct); - helpMenu->addAction(aboutQtAct); - -// createToolBars(); - fileToolBar = addToolBar(tr("File")); - fileToolBar->addAction(newAct); - fileToolBar->addAction(openAct); - fileToolBar->addAction(saveAct); - - editToolBar = addToolBar(tr("Edit")); - editToolBar->addAction(cutAct); - editToolBar->addAction(copyAct); - editToolBar->addAction(pasteAct); -#else CreateActions(); CreateMenus(); CreateToolbars(); -#endif // Create status bar scaleIndicator = new QLabel("Scale: 100%"); @@ -116,7 +69,7 @@ MainWindow::MainWindow() // setCurrentFile(""); setUnifiedTitleAndToolBarOnMac(true); - ((TTEdit *)qApp)->charWnd->show();//eh? + Global::charWnd->show(); } @@ -206,8 +159,8 @@ void MainWindow::closeEvent(QCloseEvent * event) void MainWindow::NewGlyph(void) { editWnd->pts.Clear(); - ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts)); - ((TTEdit *)qApp)->charWnd->update(); + Global::charWnd->MakePathFromPoints(&(editWnd->pts)); + Global::charWnd->update(); filename.clear(); // editWnd->polyFirstPoint = true; editWnd->update(); @@ -218,8 +171,19 @@ void MainWindow::NewGlyph(void) void MainWindow::OpenFile(void) { +#if 0 filename = QFileDialog::getOpenFileName(this, tr("Open Glyph File"), "./", tr("Glyph files (*.glyph)")); +#else + PreviewFileDialog * pfd = new PreviewFileDialog(this, tr("Open Glyph File"), + "./", tr("Glyph files (*.glyph)")); + pfd->setAcceptMode(QFileDialog::AcceptOpen); + + if (pfd->exec() == false) + return; + + filename = pfd->selectedFiles().at(0); +#endif FILE * file = fopen(filename.toUtf8().data(), "r"); //need to pop an error box here... @@ -229,8 +193,8 @@ void MainWindow::OpenFile(void) editWnd->pts.LoadGlyphFromFile(file); fclose(file); - ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts)); - ((TTEdit *)qApp)->charWnd->update(); + Global::charWnd->MakePathFromPoints(&(editWnd->pts)); + Global::charWnd->update(); editWnd->update(); setWindowTitle(QString("TTEdit! - %1").arg(filename)); editWnd->setFocus(); @@ -300,8 +264,8 @@ void MainWindow::ReadSettings(void) move(pos); pos = settings.value("charWndPos", QPoint(0, 0)).toPoint(); size = settings.value("charWndSize", QSize(200, 200)).toSize(); - ((TTEdit *)qApp)->charWnd->resize(size); - ((TTEdit *)qApp)->charWnd->move(pos); + Global::charWnd->resize(size); + Global::charWnd->move(pos); } @@ -310,8 +274,8 @@ void MainWindow::WriteSettings(void) QSettings settings("Underground Software", "TTEdit"); settings.setValue("pos", pos()); settings.setValue("size", size()); - settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos()); - settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size()); + settings.setValue("charWndPos", Global::charWnd->pos()); + settings.setValue("charWndSize", Global::charWnd->size()); } diff --git a/src/previewfiledialog.cpp b/src/previewfiledialog.cpp new file mode 100644 index 0000000..b354247 --- /dev/null +++ b/src/previewfiledialog.cpp @@ -0,0 +1,68 @@ +#include "previewfiledialog.h" +#include +#include +#include +#include +#include "charwindow.h" +#include "global.h" +#include "glyphpoints.h" + + +PreviewFileDialog::PreviewFileDialog( + QWidget * parent, + const QString & caption, + const QString & directory, + const QString & filter): + QFileDialog(parent, caption, directory, filter) +{ + setObjectName("PreviewFileDialog"); + QVBoxLayout * box = new QVBoxLayout; + + mpPreview = new QLabel(tr("Preview"), this); + mpPreview->setAlignment(Qt::AlignCenter); + mpPreview->setObjectName("labelPreview"); + box->addWidget(mpPreview); + + box->addStretch(); + + // add to QFileDialog layout + { + QGridLayout * layout = (QGridLayout *)this->layout(); + layout->addLayout(box, 1, 3, 3, 1); + } + + connect(this, SIGNAL(currentChanged(const QString &)), this, SLOT(HandleCurrentChanged(const QString &))); +} + + +void PreviewFileDialog::HandleCurrentChanged(const QString & path) +{ + if (path.endsWith(".glyph") == false) + return; + + GlyphPoints gp; + QPainterPath * glyphPath = NULL; + FILE * fp = fopen(path.toUtf8().data(), "r"); + + if (fp != NULL) + { + if (gp.LoadGlyphFromFile(fp) == true) + glyphPath = Global::charWnd->MakePathFromPoints(&gp); + + fclose(fp); + } + + QImage glyphImg(160, 160, QImage::Format_RGBA8888); + glyphImg.fill(Qt::transparent); + Global::charWnd->RenderPathInImage(glyphPath, &glyphImg); + QPixmap pixmap = QPixmap::fromImage(glyphImg); + + if (pixmap.isNull() == false) + { + mpPreview->setPixmap(pixmap);//.scaled(mpPreview->width(), mpPreview->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); +// return; + } + +// mpPreview->setText(QString("not an image"));//
%1").arg(path)); +} + diff --git a/src/previewfiledialog.h b/src/previewfiledialog.h new file mode 100644 index 0000000..78a693b --- /dev/null +++ b/src/previewfiledialog.h @@ -0,0 +1,26 @@ +#ifndef __PREVIEWFILEDIALOG_H__ +#define __PREVIEWFILEDIALOG_H__ + +#include + +class QLabel; + +class PreviewFileDialog: public QFileDialog +{ + Q_OBJECT + + public: + PreviewFileDialog(QWidget * parent = 0, + const QString & caption = QString(), + const QString & directory = QString(), + const QString & filter = QString()); + + protected slots: + void HandleCurrentChanged(const QString & path); + + protected: + QLabel * mpPreview; +}; + +#endif // __PREVIEWFILEDIALOG_H__ + diff --git a/src/toolwindow.h b/src/toolwindow.h index 2864970..6fc3723 100644 --- a/src/toolwindow.h +++ b/src/toolwindow.h @@ -13,19 +13,19 @@ // Enumerations enum ToolType { - TOOLNone = -1, // No tool - TOOLSelect = 0, // The "selection" tool - TOOLPolySelect, // Polygon selection tool - TOOLMultiSelect, // Rectangle selection tool - TOOLZoom, // Zoom window tool - TOOLAddPt, // Add point tool - TOOLAddPoly, // Polygon creation tool - TOOLDelPt, // Delete point tool - TOOLDelPoly, // Delete polygon tool - TOOLRotate, // Rotate tool - TOOLRotatePoly, // Rotate polygon around centroid tool - TOOLFlipWinding, // Change polygon's winding direction tool - TOOLScroll // Scroll window tool + TOOLNone = -1, // No tool + TOOLSelect = 0, // The "selection" tool + TOOLPolySelect, // Polygon selection tool + TOOLMultiSelect, // Rectangle selection tool + TOOLZoom, // Zoom window tool + TOOLAddPt, // Add point tool + TOOLAddPoly, // Polygon creation tool + TOOLDelPt, // Delete point tool + TOOLDelPoly, // Delete polygon tool + TOOLRotate, // Rotate tool + TOOLRotatePoly, // Rotate polygon around centroid tool + TOOLFlipWinding, // Change polygon's winding direction tool + TOOLScroll // Scroll window tool }; class ToolWindow: public QWidget diff --git a/src/ttedit.cpp b/src/ttedit.cpp index 46e9943..f023f03 100644 --- a/src/ttedit.cpp +++ b/src/ttedit.cpp @@ -18,15 +18,15 @@ #include "ttedit.h" #include +#include "global.h" #include "mainwindow.h" // Main app constructor--we stick globally accessible stuff here... -TTEdit::TTEdit(int & argc, char * argv[]): QApplication(argc, argv), charWnd(NULL) +TTEdit::TTEdit(int & argc, char * argv[]): QApplication(argc, argv) { - mainWindow = new MainWindow; -//printf("mainWindow.show();\n"); - mainWindow->show(); + Global::mainWindow = new MainWindow; + Global::mainWindow->show(); } @@ -35,11 +35,6 @@ int main(int argc, char * argv[]) { Q_INIT_RESOURCE(ttedit); // This must the same name as the exe filename -// QApplication app(argc, argv); -//printf("TTEdit app(argc, argv);\n"); TTEdit app(argc, argv); -//printf("TTEMainWindow mainWindow;\n"); -//OK, it gets to here at least... -//printf("return app.exec();\n"); return app.exec(); } diff --git a/src/ttedit.h b/src/ttedit.h index 482b6ac..7713a10 100644 --- a/src/ttedit.h +++ b/src/ttedit.h @@ -11,8 +11,8 @@ #include // Forward declarations -class CharWindow; -class MainWindow; +//class CharWindow; +//class MainWindow; class TTEdit: public QApplication { @@ -20,8 +20,8 @@ class TTEdit: public QApplication TTEdit(int & argc, char * argv[]); public: - CharWindow * charWnd; - MainWindow * mainWindow; +// CharWindow * charWnd; +// MainWindow * mainWindow; }; #endif // __TTEDIT_H__ diff --git a/ttedit.pro b/ttedit.pro index 426123b..d82bb06 100644 --- a/ttedit.pro +++ b/ttedit.pro @@ -14,6 +14,7 @@ HEADERS += src/list.h HEADERS += src/mainwindow.h HEADERS += src/mathconstants.h HEADERS += src/painter.h +HEADERS += src/previewfiledialog.h HEADERS += src/toolwindow.h HEADERS += src/ttedit.h HEADERS += src/vector.h @@ -25,6 +26,7 @@ SOURCES += src/global.cpp SOURCES += src/glyphpoints.cpp SOURCES += src/mainwindow.cpp SOURCES += src/painter.cpp +SOURCES += src/previewfiledialog.cpp SOURCES += src/toolwindow.cpp SOURCES += src/ttedit.cpp SOURCES += src/vector.cpp