#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;
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)));
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));
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)
{
// 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;
}
p.drawPath(*path);
}
+
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 <wx/wx.h> // So that whoever uses this can without having
- // to pull in a bunch of references manually
-#include <wx/minifram.h>
-
-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__
+
#include "debug.h"
#include "global.h"
#include "mainwindow.h"
+#include "mathconstants.h"
#include "ttedit.h"
#include "vector.h"
}
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,
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();
}
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)
{
//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();
}
std::vector<bool> Global::layerHidden;
std::vector<bool> Global::layerLocked;
+CharWindow * Global::charWnd = 0;
+MainWindow * Global::mainWindow = 0;
+
#include "vector.h"
class QFont;
+class CharWindow;
+class MainWindow;
enum LineStyle { LSNone, LSSolid, LSDash, LSDot, LSDashDot, LSDashDotDot };
static int numLayers;
static std::vector<bool> layerHidden;
static std::vector<bool> layerLocked;
+
+ static CharWindow * charWnd;
+ static MainWindow * mainWindow;
};
#endif // __GLOBALS_H__
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; i<numPoints; i++)
{
- fscanf(file, "%d %d %s", &x[i], &y[i], (char *)&line);
+ fscanf(file, "%d %d %s\n", &x[i], &y[i], (char *)&line);
onCurve[i] = (line[0] == 'T' ? true : false);
}
- fscanf(file, "%s %u", line, &numPolys);
+ num = fscanf(file, "POLYS %u\n", &numPolys);
polyEnd = new uint16_t[numPolys];
for(int i=0; i<numPolys; i++)
{
- fscanf(file, "%u", &polyEnd[i]);
+ fscanf(file, "%u\n", &polyEnd[i]);
}
return true;
#include "charwindow.h"
#include "editwindow.h"
#include "global.h"
+#include "previewfiledialog.h"
#include "ttedit.h"
MainWindow::MainWindow()
{
- ((TTEdit *)qApp)->charWnd = 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%");
// setCurrentFile("");
setUnifiedTitleAndToolBarOnMac(true);
- ((TTEdit *)qApp)->charWnd->show();//eh?
+ Global::charWnd->show();
}
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();
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...
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();
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);
}
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());
}
--- /dev/null
+#include "previewfiledialog.h"
+#include <QLabel>
+#include <QGridLayout>
+#include <QPainterPath>
+#include <QPainter>
+#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"));//<br>%1").arg(path));
+}
+
--- /dev/null
+#ifndef __PREVIEWFILEDIALOG_H__
+#define __PREVIEWFILEDIALOG_H__
+
+#include <QFileDialog>
+
+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__
+
// 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
#include "ttedit.h"
#include <QApplication>
+#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();
}
{
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();
}
#include <QtWidgets>
// Forward declarations
-class CharWindow;
-class MainWindow;
+//class CharWindow;
+//class MainWindow;
class TTEdit: public QApplication
{
TTEdit(int & argc, char * argv[]);
public:
- CharWindow * charWnd;
- MainWindow * mainWindow;
+// CharWindow * charWnd;
+// MainWindow * mainWindow;
};
#endif // __TTEDIT_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
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