├── zh_CN.qm ├── Graph.ico ├── icon.qrc ├── imageaction.h ├── tddraw.h ├── colorwidget.cpp ├── GraphVector.h ├── fillshape.h ├── ActionType.h ├── colorlabel.h ├── graphmove.h ├── main.cpp ├── imageaction.cpp ├── graphtool.h ├── graphscaled.h ├── graphrevolve.h ├── ccohen_sutherland.h ├── Readme.md ├── icon.rc ├── myopengl.h ├── Graph.pro ├── colorlabel.cpp ├── fillshape.cpp ├── graphtool.cpp ├── mainwindow.h ├── painterwidget.h ├── graphscaled.cpp ├── graphmove.cpp ├── myopengl.cpp ├── graphrevolve.cpp ├── tddraw.cpp ├── ccohen_sutherland.cpp ├── mainwindow.cpp ├── mainwindow.ui ├── zh_CN.ts ├── painterwidget.cpp ├── Graph.pro.user.3.3-pre1 └── Graph.pro.user /zh_CN.qm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loveyu/simple-graph/master/zh_CN.qm -------------------------------------------------------------------------------- /Graph.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loveyu/simple-graph/master/Graph.ico -------------------------------------------------------------------------------- /icon.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | Graph.ico 4 | 5 | 6 | -------------------------------------------------------------------------------- /imageaction.h: -------------------------------------------------------------------------------- 1 | #ifndef IMAGEACTION_H 2 | #define IMAGEACTION_H 3 | #include 4 | #include 5 | 6 | class ImageAction 7 | { 8 | public: 9 | static void delete_rect(QRect rect,QImage &img,QColor color); 10 | static void fill(int x,int y,QImage &img,QColor color); 11 | }; 12 | 13 | #endif // IMAGEACTION_H 14 | -------------------------------------------------------------------------------- /tddraw.h: -------------------------------------------------------------------------------- 1 | #ifndef TDCUBE_H 2 | #define TDCUBE_H 3 | #include 4 | #include 5 | class TdDraw 6 | { 7 | public: 8 | TdDraw(); 9 | ~TdDraw(); 10 | void Cube(QMatrix4x4 r); 11 | void Vertebral(QMatrix4x4 r); 12 | void Default(QMatrix4x4 r); 13 | private: 14 | void init(); 15 | }; 16 | 17 | #endif // TDCUBE_H 18 | -------------------------------------------------------------------------------- /colorwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "colorwidget.h" 2 | 3 | ColorWidget::ColorWidget(QWidget *parent) : QWidget(parent) 4 | { 5 | resize(parent->width(),parent->height()); 6 | } 7 | 8 | ColorWidget::~ColorWidget() 9 | { 10 | 11 | } 12 | 13 | void ColorWidget::paintEvent(QPaintEvent *){ 14 | QPainter painter(this); 15 | painter.fillRect(this->rect(),Qt::SolidPattern); 16 | } 17 | -------------------------------------------------------------------------------- /GraphVector.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHVECTOR_H 2 | #define GRAPHVECTOR_H 3 | #include 4 | #include 5 | 6 | struct V_Line{ 7 | int x1,y1,x2,y2; 8 | }; 9 | 10 | struct V_Circle{ 11 | int x,y,r,r2; 12 | }; 13 | 14 | struct V_Triangle{ 15 | int x1,x2,x3,y1,y2,y3; 16 | }; 17 | 18 | struct CP2{ 19 | int x,y,rc; 20 | }; 21 | 22 | struct XyPoint{ 23 | int x,y; 24 | }; 25 | 26 | #endif // GRAPHVECTOR_H 27 | -------------------------------------------------------------------------------- /fillshape.h: -------------------------------------------------------------------------------- 1 | #ifndef FILLSHAPE_H 2 | #define FILLSHAPE_H 3 | #include 4 | #include "GraphVector.h" 5 | #include 6 | #include 7 | class FillShape 8 | { 9 | public: 10 | FillShape(QImage *img,QColor c); 11 | void fill(int x,int y); 12 | private: 13 | QImage *img; 14 | int w,h; 15 | QRgb color; 16 | QStack stack; 17 | bool isBrim(int x,int y); 18 | void fillStack(XyPoint p); 19 | }; 20 | 21 | #endif // FILLSHAPE_H 22 | -------------------------------------------------------------------------------- /ActionType.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIONTYPE_H 2 | #define ACTIONTYPE_H 3 | 4 | enum ActionType{ 5 | Normal, 6 | D_Line, 7 | D_Circle, 8 | D_Oval, 9 | D_Rect, 10 | D_Triangle, 11 | D_Polygon, 12 | O_LineCut, 13 | O_Move, 14 | O_Revolve, 15 | O_Scaled, 16 | M_Move, 17 | M_Rotate, 18 | M_Scaled, 19 | M_Reset, 20 | E_Erase, 21 | E_Fill, 22 | TD_Normal, 23 | TD_Cube, 24 | TD_Vertebral 25 | }; 26 | 27 | #endif // ACTIONTYPE_H 28 | -------------------------------------------------------------------------------- /colorlabel.h: -------------------------------------------------------------------------------- 1 | #ifndef COLORLABEL_H 2 | #define COLORLABEL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class ColorLabel : public QLabel 9 | { 10 | Q_OBJECT 11 | public: 12 | explicit ColorLabel(QWidget *parent = 0); 13 | ~ColorLabel(); 14 | void setColor(QColor color); 15 | QColor getColor(); 16 | protected: 17 | void mousePressEvent(QMouseEvent *e); 18 | void paintEvent(QPaintEvent *e); 19 | QWidget *parent; 20 | QColor color; 21 | }; 22 | 23 | #endif // COLORLABEL_H 24 | -------------------------------------------------------------------------------- /graphmove.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHMOVE_H 2 | #define GRAPHMOVE_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "GraphVector.h" 8 | class GraphMove 9 | { 10 | public: 11 | GraphMove(int x,int y); 12 | bool is_move(int x,int y); 13 | void move(QList &line, 14 | QList &circle, 15 | QList &oval, 16 | QList &rect, 17 | QList &triangle, 18 | QList &polygon); 19 | private : 20 | int x,y; 21 | int o_x,o_y; 22 | QRectF moveRectF(QRectF rect); 23 | QPolygonF movePolygon(QPolygonF polygon); 24 | }; 25 | 26 | #endif // GRAPHMOVE_H 27 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | QTextCodec *xcodec = QTextCodec::codecForLocale() ; 10 | QString exeDir = xcodec->toUnicode( QByteArray(argv[0]) ) ; 11 | QString BKE_CURRENT_DIR = QFileInfo( exeDir ).path() ; 12 | QApplication::setLibraryPaths( QApplication::libraryPaths() << BKE_CURRENT_DIR) ; 13 | 14 | QApplication a(argc, argv); 15 | QTranslator appTranslator; 16 | 17 | appTranslator.load("zh_CN"); 18 | a.installTranslator(&appTranslator); 19 | 20 | MainWindow w; 21 | w.show(); 22 | 23 | return a.exec(); 24 | } 25 | -------------------------------------------------------------------------------- /imageaction.cpp: -------------------------------------------------------------------------------- 1 | #include "imageaction.h" 2 | #include 3 | #include 4 | #include "fillshape.h" 5 | 6 | void ImageAction::delete_rect(QRect rect,QImage &img,QColor color){ 7 | QRgb c = qRgba(color.red(), color.green(), color.blue(), color.alpha()); 8 | int w = img.width(),h = img.height(); 9 | for (short i = rect.top(); i < rect.top() + rect.height(); i++) { 10 | for (short j = rect.left(); j < rect.left() + rect.width(); j++) { 11 | if(i<0 || j<0 || j>=w || i>=h)continue; 12 | img.setPixel(j, i, c); 13 | } 14 | } 15 | } 16 | 17 | void ImageAction::fill(int x,int y, QImage &img,QColor color){ 18 | FillShape fs(&img,color); 19 | fs.fill(x,y); 20 | } 21 | -------------------------------------------------------------------------------- /graphtool.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHTOOL_H 2 | #define GRAPHTOOL_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "colorlabel.h" 9 | 10 | class GraphTool : public QToolBar 11 | { 12 | Q_OBJECT 13 | public: 14 | explicit GraphTool(QWidget *parent = 0); 15 | ~GraphTool(); 16 | void setAction(ActionType type,void *value); 17 | void init(); 18 | ColorLabel * getColor(); 19 | public slots: 20 | void setSpinBox(int value); 21 | private: 22 | QSpinBox *spin_box=NULL; 23 | ColorLabel *color_box=NULL; 24 | MainWindow *main; 25 | QAction *spin_action; 26 | QAction *color_action; 27 | }; 28 | 29 | #endif // GRAPHTOOL_H 30 | -------------------------------------------------------------------------------- /graphscaled.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHSCALED_H 2 | #define GRAPHSCALED_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "GraphVector.h" 9 | 10 | class GraphScaled 11 | { 12 | public: 13 | GraphScaled(); 14 | ~GraphScaled(); 15 | void setTranslate(int x,int y); 16 | void scaled(double re,QList &line, 17 | QList &oval, 18 | QList &circle, 19 | QList &rect, 20 | QList &triangle, 21 | QList &polygon); 22 | private: 23 | int x,y; 24 | QMatrix matrix; 25 | void triangleAddQPolygonF(QList triangle,QList &polygon); 26 | }; 27 | 28 | #endif // GRAPHSCALED_H 29 | -------------------------------------------------------------------------------- /graphrevolve.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHREVOLVE_H 2 | #define GRAPHREVOLVE_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "GraphVector.h" 9 | class GraphRevolve 10 | { 11 | public: 12 | GraphRevolve(); 13 | void setTranslate(int x,int y); 14 | void revolve(double re,QList &line, 15 | QList &circle, 16 | QList &rect, 17 | QList &triangle, 18 | QList &polygon); 19 | QMatrix matrix; 20 | private : 21 | int x,y; 22 | void triangleAddQPolygonF(QList triangle,QList &polygon); 23 | void rectAddQPolygonF(QList triangle,QList &polygon); 24 | }; 25 | 26 | #endif // GRAPHREVOLVE_H 27 | -------------------------------------------------------------------------------- /ccohen_sutherland.h: -------------------------------------------------------------------------------- 1 | #ifndef CCOHEN_SUTHERLAND_H 2 | #define CCOHEN_SUTHERLAND_H 3 | #include "GraphVector.h" 4 | #include 5 | #include 6 | 7 | #define CLIP_CODE_C 0x0000 8 | #define CLIP_CODE_W 0x0001 9 | #define CLIP_CODE_E 0x0002 10 | #define CLIP_CODE_S 0x0004 11 | #define CLIP_CODE_N 0x0008 12 | #define CLIP_CODE_SW 0x0005 13 | #define CLIP_CODE_SE 0x0006 14 | #define CLIP_CODE_NW 0x0009 15 | #define CLIP_CODE_NE 0x000a 16 | 17 | class CCohen_Sutherland 18 | { 19 | public: 20 | CCohen_Sutherland(V_Line rect); 21 | void cutLine(QList &list_line); 22 | private: 23 | V_Line rect; 24 | int cut(int LineX1,int LineY1,int LineX2,int LineY2 , 25 | int RectX1,int RectY1,int RectX2,int RectY2 , 26 | int &LineX3,int &LineY3,int &LineX4,int &LineY4 ); 27 | }; 28 | 29 | 30 | #endif // CCOHEN_SUTHERLAND_H 31 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | ## 程序说明 2 | * 程序所在目录要保持结构不变,可以完整的独立在32位或64位系统上运行 3 | * 程序源码为标准的QT5.2工程文件,编译器需C++11支持 4 | 5 | ## 程序运行说明 6 | * 默认状态下,打开的程序主窗口只能拖一个虚线框,需点击绘图菜单,然后选择对应功能方可继续使用 7 | * 在绘制图形过程中,都是以鼠标和键盘操作 8 | * 绘制直线:点击一点不放,移动到另一点,直线随之移动,释放完成绘制 9 | * 绘制矩形:点击任意一点,拖动不放,到另一点,绘制完成 10 | * 绘制正方形:和矩形类似,期间按住`Shift`键 11 | * 绘制圆(椭圆):点击任意一点拖动 12 | * 绘制三角形:先以画直线方式画一条直线,移动鼠标,会出现另两条虚线,点击完成三角形绘制 13 | * 绘制多边形:类似画三角方式,只是多边形不会自动封闭,如果画五边形,则在画完三条边后按下`ESC`键,移动鼠标补充另外两条线,任意多边形同理。 14 | * 直线段裁剪:点击后使用虚线框选中,然后只保留在框中的直线 15 | * 缩放:缩放过程中,必须先确定缩放中心,即用鼠标点击的圆圈,然后用鼠标滚动 16 | * 旋转:同缩放,只是椭圆无法旋转 17 | * 移动:点击任意位置,不放,移动鼠标,同时移动对象 18 | * 矩阵操作:旋转、移动、缩放和之前的类似又有不同,支持任意对象的旋转。操作结束后应重置 19 | * 擦除:先将图像光栅化(即位图),然后可擦除,可指定擦除颜色,如果纯透明(机颜色Alpha值)的黑色为空白,即背景状态,该颜色可填充。 20 | * 填充:在位图状态下,选择指定颜色,点击指定空白,稍等片刻完成填充,只对空白背景有效 21 | * 图像保存:选择指定路径和文件名,将保存为一个透明背景图片。 22 | * 3D:先转为3D视图,在该模式下可选择3个不同的3D模型。通过鼠标点击后移动,进行旋转,旋转操作为定时事件,将以一定的减速度旋转,直到停止 23 | 24 | ## 关于 25 | * loveyu [(**恋羽**)](http://www.loveyu.org/) 26 | * [http://www.loveyu.org/3842.html](http://www.loveyu.org/3842.html) -------------------------------------------------------------------------------- /icon.rc: -------------------------------------------------------------------------------- 1 | #if defined(UNDER_CE) 2 | #include 3 | #else 4 | #include 5 | #endif 6 | VS_VERSION_INFO VERSIONINFO 7 | FILEVERSION 1,0,0,0 8 | PRODUCTVERSION 1,0,0,0 9 | FILEFLAGSMASK 0x3fL 10 | #ifdef _DEBUG 11 | FILEFLAGS VS_FF_DEBUG 12 | #else 13 | FILEFLAGS 0x0L 14 | #endif 15 | FILEOS VOS__WINDOWS32 16 | FILETYPE VFT_DLL 17 | FILESUBTYPE 0x0L 18 | BEGIN 19 | BLOCK "StringFileInfo" 20 | BEGIN 21 | BLOCK "080404b0" 22 | BEGIN 23 | VALUE "CompanyName", "Loveyu" 24 | VALUE "FileDescription", "Simple Graph Edit" 25 | VALUE "FileVersion", "1.0.0.0" 26 | VALUE "InternalName", "Graph.exe" 27 | VALUE "LegalCopyright", "Copyright (C)2014" 28 | VALUE "OriginalFilename", "Graph.exe" 29 | VALUE "ProductName", "Simple Graph Edit" 30 | VALUE "ProductVersion", "1.0.0.0" 31 | END 32 | END 33 | BLOCK "VarFileInfo" 34 | BEGIN 35 | VALUE "Translation", 0x804, 1200 36 | END 37 | END 38 | IDI_ICON1 ICON DISCARDABLE "Graph.ico" 39 | -------------------------------------------------------------------------------- /myopengl.h: -------------------------------------------------------------------------------- 1 | #ifndef MYOPENGL_H 2 | #define MYOPENGL_H 3 | #include "ActionType.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "tddraw.h" 17 | 18 | class MyOpenGL : public QOpenGLWidget 19 | { 20 | Q_OBJECT 21 | public: 22 | explicit MyOpenGL(QWidget *parent = 0); 23 | ~MyOpenGL(); 24 | void setActionType(ActionType type); 25 | protected: 26 | void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; 27 | void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE; 28 | void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE; 29 | 30 | void initializeGL(); 31 | void paintGL(); 32 | void resizeGL(int w, int h); 33 | private: 34 | ActionType type; 35 | TdDraw *draw=NULL; 36 | 37 | QMatrix4x4 projection; 38 | QVector2D mousePressPosition; 39 | QVector3D rotationAxis; 40 | qreal angularSpeed; 41 | QQuaternion rotation; 42 | QBasicTimer timer; 43 | }; 44 | 45 | #endif // MYOPENGL_H 46 | -------------------------------------------------------------------------------- /Graph.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-12-09T08:24:16 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Graph 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | mainwindow.cpp \ 17 | painterwidget.cpp \ 18 | ccohen_sutherland.cpp \ 19 | graphmove.cpp \ 20 | graphrevolve.cpp \ 21 | graphscaled.cpp \ 22 | graphtool.cpp \ 23 | imageaction.cpp \ 24 | fillshape.cpp \ 25 | myopengl.cpp \ 26 | tddraw.cpp \ 27 | colorlabel.cpp 28 | 29 | HEADERS += mainwindow.h \ 30 | painterwidget.h \ 31 | ActionType.h \ 32 | GraphVector.h \ 33 | ccohen_sutherland.h \ 34 | graphmove.h \ 35 | graphrevolve.h \ 36 | graphscaled.h \ 37 | graphtool.h \ 38 | imageaction.h \ 39 | fillshape.h \ 40 | myopengl.h \ 41 | tddraw.h \ 42 | colorlabel.h 43 | 44 | FORMS += mainwindow.ui 45 | 46 | CONFIG += c++11 47 | 48 | RESOURCES += \ 49 | icon.qrc 50 | RC_FILE += icon.rc 51 | 52 | OTHER_FILES += \ 53 | icon.rc 54 | 55 | TRANSLATIONS = zh_CN.ts 56 | -------------------------------------------------------------------------------- /colorlabel.cpp: -------------------------------------------------------------------------------- 1 | #include "colorlabel.h" 2 | #include 3 | #include 4 | 5 | ColorLabel::ColorLabel(QWidget *parent) : 6 | QLabel(parent) 7 | { 8 | this->parent = parent; 9 | setText(" "); 10 | setStyleSheet("background-color: rgb(0, 0, 0);"); 11 | } 12 | 13 | QColor ColorLabel::getColor(){ 14 | return color; 15 | } 16 | 17 | ColorLabel::~ColorLabel() 18 | { 19 | 20 | } 21 | void ColorLabel::mousePressEvent(QMouseEvent *event){ 22 | event->accept(); 23 | color = QColorDialog::getColor(color, this->parent,tr("Select you draw color"),QColorDialog::ShowAlphaChannel); 24 | setColor(color); 25 | } 26 | 27 | void ColorLabel::paintEvent(QPaintEvent *e){ 28 | e->accept(); 29 | QPainter p(this); 30 | p.drawRect(0,0,width()-1,height()-1); 31 | } 32 | 33 | void ColorLabel::setColor(QColor color){ 34 | this->color = color; 35 | setStyleSheet(QString("background-color: rgba(%1, %2, %3, %4);").arg(QString::number(color.red()), 36 | QString::number(color.green()), 37 | QString::number(color.blue()), 38 | QString::number(color.alpha()))); 39 | update(); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /fillshape.cpp: -------------------------------------------------------------------------------- 1 | #include "fillshape.h" 2 | FillShape::FillShape(QImage *img,QColor c) 3 | { 4 | w = img->width(); 5 | h = img->height(); 6 | this->img = img; 7 | color = qRgba(c.red(), c.green(), c.blue(), c.alpha()); 8 | } 9 | 10 | bool FillShape::isBrim(int x,int y){ 11 | if(x<=0 || y<=0 || x>=w || y>=h){ 12 | return true; 13 | } 14 | QRgb rgb =img->pixel(QPoint(x,y)); 15 | return qAlpha(rgb)!=0 && rgb>0; 16 | } 17 | 18 | void FillShape::fillStack(XyPoint p){ 19 | if(qAlpha(color)==0){ 20 | return; 21 | } 22 | XyPoint tmp; 23 | img->setPixel(p.x,p.y,color); 24 | if(!isBrim(p.x,p.y+1)){ 25 | tmp.x = p.x; 26 | tmp.y = p.y+1; 27 | stack.push(tmp); 28 | } 29 | if(!isBrim(p.x,p.y-1)){ 30 | tmp.x = p.x; 31 | tmp.y = p.y-1; 32 | stack.push(tmp); 33 | } 34 | if(!isBrim(p.x-1,p.y)){ 35 | tmp.x = p.x-1; 36 | tmp.y = p.y; 37 | stack.push(tmp); 38 | } 39 | if(!isBrim(p.x+1,p.y)){ 40 | tmp.x = p.x+1; 41 | tmp.y = p.y; 42 | stack.push(tmp); 43 | } 44 | } 45 | 46 | void FillShape::fill(int x,int y){ 47 | if(isBrim(x,y)){ 48 | return; 49 | } 50 | stack.clear(); 51 | XyPoint p; 52 | p.x = x; 53 | p.y = y; 54 | fillStack(p); 55 | while(!stack.isEmpty()){ 56 | fillStack(stack.pop()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /graphtool.cpp: -------------------------------------------------------------------------------- 1 | #include "graphtool.h" 2 | #include "painterwidget.h" 3 | 4 | GraphTool::GraphTool(QWidget *parent) : 5 | QToolBar(parent) 6 | { 7 | main = (MainWindow *)parent; 8 | spin_box = new QSpinBox(); 9 | spin_box->setRange(1,1000); 10 | spin_box->setPrefix(tr("Radius:")); 11 | spin_box->setToolTip(tr("Radius")); 12 | connect(spin_box,SIGNAL(valueChanged(int)),this,SLOT(setSpinBox(int))); 13 | color_box = new ColorLabel(this); 14 | } 15 | 16 | void GraphTool::init(){ 17 | spin_action = addWidget(spin_box); 18 | color_action = addWidget(color_box); 19 | spin_action->setVisible(false); 20 | addAction(spin_action); 21 | addAction(color_action); 22 | } 23 | 24 | ColorLabel * GraphTool::getColor(){ 25 | return color_box; 26 | } 27 | 28 | void GraphTool::setSpinBox(int value){ 29 | switch(main->getActionType()){ 30 | case E_Erase: 31 | ((PainterWidget *)main->getPainterWidget())->setCircleR(value,value); 32 | break; 33 | default: 34 | break; 35 | } 36 | } 37 | 38 | void GraphTool::setAction(ActionType type,void *value){ 39 | spin_action->setVisible(false); 40 | int *v; 41 | switch (type) { 42 | case E_Erase: 43 | v = (int *)value; 44 | spin_box->setValue(*v); 45 | spin_action->setVisible(true); 46 | break; 47 | default: 48 | break; 49 | } 50 | } 51 | 52 | GraphTool::~GraphTool() 53 | { 54 | delete spin_box; 55 | delete color_box; 56 | } 57 | -------------------------------------------------------------------------------- /mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "ActionType.h" 9 | #include 10 | 11 | namespace Ui { 12 | class MainWindow; 13 | } 14 | 15 | class MainWindow : public QMainWindow 16 | { 17 | Q_OBJECT 18 | 19 | public: 20 | explicit MainWindow(QWidget *parent = 0); 21 | ~MainWindow(); 22 | void setActionType(ActionType type); 23 | ActionType getActionType(); 24 | QLabel *status_mouse; 25 | QLabel *status_action; 26 | QWidget * getPainterWidget(); 27 | protected: 28 | void resizeEvent(QResizeEvent *event); 29 | private slots: 30 | void on_actionLine_triggered(); 31 | 32 | void on_actionCircle_triggered(); 33 | 34 | void on_actionOval_triggered(); 35 | 36 | void on_actionPolygon_triggered(); 37 | 38 | void on_actionTriangle_triggered(); 39 | 40 | void on_actionClear_triggered(); 41 | 42 | void on_actionRect_triggered(); 43 | 44 | void on_actionLineCut_triggered(); 45 | 46 | void on_actionMove_triggered(); 47 | 48 | void on_actionRevolve_triggered(); 49 | 50 | void on_actionScaled_triggered(); 51 | 52 | void on_actionSave_triggered(); 53 | 54 | void on_actionMReset_triggered(); 55 | 56 | void on_actionMScaled_triggered(); 57 | 58 | void on_actionMRotate_triggered(); 59 | 60 | void on_actionMMove_triggered(); 61 | 62 | void on_actionToBitmap_triggered(); 63 | 64 | void on_actionErase_triggered(); 65 | 66 | void on_actionFill_triggered(); 67 | 68 | void on_actionTo3D_triggered(); 69 | 70 | void on_actionCube_triggered(); 71 | 72 | void on_actionVertebral_triggered(); 73 | 74 | void on_action3DRect_triggered(); 75 | 76 | void on_actionAuthor_triggered(); 77 | 78 | void on_actionInfo_triggered(); 79 | 80 | void on_actionExit_triggered(); 81 | 82 | private: 83 | Ui::MainWindow *ui; 84 | ActionType action_type=Normal; 85 | bool is_3d=false; 86 | bool checkIs3D(); 87 | }; 88 | 89 | #endif // MAINWINDOW_H 90 | -------------------------------------------------------------------------------- /painterwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef PAINTERWIDGET_H 2 | #define PAINTERWIDGET_H 3 | 4 | #include "mainwindow.h" 5 | #include "GraphVector.h" 6 | #include "ccohen_sutherland.h" 7 | #include "graphmove.h" 8 | #include "graphrevolve.h" 9 | #include "graphscaled.h" 10 | #include 11 | #include 12 | #include 13 | #include "colorlabel.h" 14 | #include 15 | using namespace std; 16 | 17 | class PainterWidget : public QWidget 18 | { 19 | Q_OBJECT 20 | public: 21 | explicit PainterWidget(QWidget *parent = 0); 22 | void setActionType(ActionType type); 23 | void clear(); 24 | void paint(QPainter &painter); 25 | void paint(QPainter &painter,bool save); 26 | bool vectorToBitmap(); 27 | bool bitmapCheck(); 28 | void setCircleR(int r,int r2); 29 | void setMainWindow(MainWindow * win,ColorLabel *c); 30 | signals: 31 | protected: 32 | void paintEvent(QPaintEvent *event); 33 | void wheelEvent(QWheelEvent *event); 34 | void mousePressEvent(QMouseEvent *event); 35 | void mouseReleaseEvent(QMouseEvent *event); 36 | void mouseMoveEvent(QMouseEvent *event); 37 | void keyPressEvent(QKeyEvent *event); 38 | void keyReleaseEvent(QKeyEvent *event); 39 | void update(); 40 | void update(bool up_img); 41 | private: 42 | MainWindow * main_window; 43 | QList list_line; 44 | QList list_circle; 45 | QList list_oval; 46 | QList list_rect; 47 | QList list_triangle; 48 | QList list_polygon; 49 | CCohen_Sutherland *line_cs; 50 | GraphMove *graph_move; 51 | GraphRevolve *graph_revolve=NULL; 52 | GraphScaled *graph_scaled=NULL; 53 | QPolygonF polygon; 54 | V_Line line; 55 | V_Line normal_rect; 56 | V_Circle circle; 57 | V_Triangle triangle; 58 | int triangle_count=0; 59 | bool press_status=false; 60 | bool press_shift = false; 61 | QPen dash_pen; 62 | double matrix_value; 63 | QMatrix matrix,matrix_backup; 64 | bool matrix_flag=false; 65 | QImage image_out; 66 | bool image_flag=false; 67 | ColorLabel *color_label; 68 | }; 69 | 70 | #endif // PAINTERWIDGET_H 71 | -------------------------------------------------------------------------------- /graphscaled.cpp: -------------------------------------------------------------------------------- 1 | #include "graphscaled.h" 2 | 3 | GraphScaled::GraphScaled() 4 | { 5 | x=y=0; 6 | } 7 | 8 | GraphScaled::~GraphScaled() 9 | { 10 | 11 | } 12 | 13 | void GraphScaled::setTranslate(int x, int y){ 14 | this->x = x; 15 | this->y = y; 16 | } 17 | 18 | void GraphScaled::scaled(double re,QList &line, 19 | QList &oval, 20 | QList &circle, 21 | QList &rect, 22 | QList &triangle, 23 | QList &polygon){ 24 | matrix = QMatrix(); 25 | matrix.translate(x,y); 26 | re=re>0?1.25:0.9; 27 | matrix.scale(re,re); 28 | matrix.translate(0-x,0-y); 29 | QList new_line; 30 | QList list_rect; 31 | QList new_polygon; 32 | QLine _line; 33 | QRectF _rectf; 34 | QPolygonF _polygonf; 35 | if(!line.isEmpty()){ 36 | for(int i=0; i< line.length();i++){ 37 | _line = line.at(i); 38 | _line=matrix.map(_line); 39 | new_line.append(_line); 40 | } 41 | line = new_line; 42 | } 43 | if(!circle.isEmpty()){ 44 | list_rect.clear(); 45 | for(int i=0; i< circle.length();i++){ 46 | _rectf = circle.at(i); 47 | _rectf = matrix.mapRect(_rectf); 48 | list_rect.append(_rectf); 49 | } 50 | circle = list_rect; 51 | } 52 | if(!oval.isEmpty()){ 53 | list_rect.clear(); 54 | for(int i=0; i< oval.length();i++){ 55 | _rectf = oval.at(i); 56 | _rectf = matrix.mapRect(_rectf); 57 | list_rect.append(_rectf); 58 | } 59 | oval = list_rect; 60 | } 61 | if(!rect.isEmpty()){ 62 | list_rect.clear(); 63 | for(int i=0; i< rect.length();i++){ 64 | _rectf = rect.at(i); 65 | _rectf = matrix.mapRect(_rectf); 66 | list_rect.append(_rectf); 67 | } 68 | rect = list_rect; 69 | } 70 | if(!triangle.isEmpty()){ 71 | triangleAddQPolygonF(triangle,polygon); 72 | triangle.clear(); 73 | } 74 | if(!polygon.isEmpty()){ 75 | for(int i=0; i< polygon.length();i++){ 76 | _polygonf = polygon.at(i); 77 | _polygonf = matrix.map(_polygonf); 78 | new_polygon.append(_polygonf); 79 | } 80 | polygon = new_polygon; 81 | } 82 | } 83 | 84 | void GraphScaled::triangleAddQPolygonF(QList triangle,QList &polygon){ 85 | V_Triangle v; 86 | for(int i = 0;ix = x; 7 | this->y = y; 8 | } 9 | bool GraphMove::is_move(int x,int y){ 10 | o_x = x-this->x; 11 | o_y = y-this->y; 12 | this->x = x; 13 | this->y = y; 14 | return true; 15 | } 16 | void GraphMove::move(QList &line, 17 | QList &circle, 18 | QList &oval, 19 | QList &rect, 20 | QList &triangle, 21 | QList &polygon){ 22 | QList new_line; 23 | QList list_rect; 24 | QList new_polygon; 25 | QLine _line; 26 | QList new_triangle; 27 | V_Triangle v_t; 28 | if(!line.isEmpty()){ 29 | for(int i=0; i< line.length();i++){ 30 | _line = line.at(i); 31 | // _line.setLine(_line.x1()+o_x,_line.y1()+o_y,_line.x2()+o_x,_line.y2()+o_y); 32 | _line.translate(o_x,o_y); 33 | new_line.append(_line); 34 | } 35 | line = new_line; 36 | } 37 | if(!circle.isEmpty()){ 38 | list_rect.clear(); 39 | for(int i=0; i< circle.length();i++){ 40 | list_rect.append(moveRectF(circle.at(i))); 41 | } 42 | circle = list_rect; 43 | } 44 | if(!oval.isEmpty()){ 45 | list_rect.clear(); 46 | for(int i=0; i< oval.length();i++){ 47 | list_rect.append(moveRectF(oval.at(i))); 48 | } 49 | oval = list_rect; 50 | } 51 | if(!rect.isEmpty()){ 52 | list_rect.clear(); 53 | for(int i=0; i< rect.length();i++){ 54 | list_rect.append(moveRectF(rect.at(i))); 55 | } 56 | rect = list_rect; 57 | } 58 | if(!polygon.isEmpty()){ 59 | for(int i=0; i< polygon.length();i++){ 60 | new_polygon.append(movePolygon(polygon.at(i))); 61 | } 62 | polygon = new_polygon; 63 | } 64 | if(!triangle.isEmpty()){ 65 | for(int i=0; i< triangle.length();i++){ 66 | v_t = triangle.at(i); 67 | v_t.x1+=o_x;v_t.x2+=o_x;v_t.x3+=o_x; 68 | v_t.y1+=o_y;v_t.y2+=o_y;v_t.y3+=o_y; 69 | new_triangle.append(v_t); 70 | } 71 | triangle = new_triangle; 72 | } 73 | 74 | } 75 | 76 | QRectF GraphMove::moveRectF(QRectF rect){ 77 | rect.translate(o_x,o_y); 78 | return rect; 79 | } 80 | 81 | QPolygonF GraphMove::movePolygon(QPolygonF polygon){ 82 | // QPointF point; 83 | // QPolygonF n_l; 84 | polygon.translate(o_x,o_y); 85 | // for(int i=0; i< polygon.length();i++){ 86 | // point = polygon.at(i); 87 | // point.setX(point.x()+o_x); 88 | // point.setY(point.y()+o_y); 89 | // n_l.append(point); 90 | // } 91 | return polygon; 92 | } 93 | 94 | 95 | -------------------------------------------------------------------------------- /myopengl.cpp: -------------------------------------------------------------------------------- 1 | #include "myopengl.h" 2 | #include 3 | #include 4 | #include 5 | 6 | MyOpenGL::MyOpenGL(QWidget *parent): 7 | QOpenGLWidget(parent) 8 | { 9 | } 10 | 11 | MyOpenGL::~MyOpenGL() 12 | { 13 | if(draw!=NULL){ 14 | delete draw; 15 | draw = NULL; 16 | } 17 | } 18 | 19 | //! [0] 20 | void MyOpenGL::mousePressEvent(QMouseEvent *e) 21 | { 22 | // Save mouse press position 23 | mousePressPosition = QVector2D(e->localPos()); 24 | } 25 | 26 | void MyOpenGL::mouseReleaseEvent(QMouseEvent *e) 27 | { 28 | // Mouse release position - mouse press position 29 | QVector2D diff = QVector2D(e->localPos()) - mousePressPosition; 30 | 31 | // Rotation axis is perpendicular to the mouse position difference 32 | // vector 33 | QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized(); 34 | 35 | // Accelerate angular speed relative to the length of the mouse sweep 36 | qreal acc = diff.length() / 100.0; 37 | 38 | // Calculate new rotation axis as weighted sum 39 | rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized(); 40 | 41 | // Increase angular speed 42 | angularSpeed += acc; 43 | } 44 | //! [0] 45 | 46 | //! [1] 47 | void MyOpenGL::timerEvent(QTimerEvent *) 48 | { 49 | // Decrease angular speed (friction) 50 | angularSpeed *= 0.99; 51 | 52 | // Stop rotation when speed goes below threshold 53 | if (angularSpeed < 0.01) { 54 | angularSpeed = 0.0; 55 | } else { 56 | // Update rotation 57 | rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation; 58 | 59 | // Request an update 60 | update(); 61 | //qDebug() << angularSpeed; 62 | } 63 | } 64 | //! [1] 65 | 66 | void MyOpenGL::paintGL(){ 67 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 68 | QMatrix4x4 matrix; 69 | matrix.rotate(rotation); 70 | 71 | switch (type) { 72 | case TD_Cube: 73 | draw->Cube(projection*matrix); 74 | break; 75 | case TD_Vertebral: 76 | draw->Vertebral(projection*matrix); 77 | break; 78 | default: 79 | draw->Default(projection*matrix); 80 | break; 81 | } 82 | } 83 | void MyOpenGL::initializeGL(){ 84 | glShadeModel( GL_SMOOTH ); 85 | glClearColor( 0.0, 0.0, 0.0, 0.0 ); 86 | glClearDepth( 1.0 ); 87 | glEnable( GL_DEPTH_TEST ); 88 | glDepthFunc( GL_LEQUAL ); 89 | glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); 90 | 91 | timer.start(12, this); 92 | } 93 | 94 | void MyOpenGL::resizeGL(int w, int h){ 95 | if ( h == 0 ) 96 | { 97 | h = 1; 98 | } 99 | glViewport( 0, 0, (GLint)w, (GLint)h ); 100 | glMatrixMode( GL_PROJECTION ); 101 | glLoadIdentity(); 102 | gluPerspective( 45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0 ); 103 | glMatrixMode( GL_MODELVIEW ); 104 | glLoadIdentity(); 105 | } 106 | 107 | 108 | void MyOpenGL::setActionType(ActionType type){ 109 | projection = QMatrix4x4(); 110 | this->type = type; 111 | update(); 112 | } 113 | -------------------------------------------------------------------------------- /graphrevolve.cpp: -------------------------------------------------------------------------------- 1 | #include "graphrevolve.h" 2 | #include 3 | 4 | GraphRevolve::GraphRevolve(){ 5 | x=y=0; 6 | } 7 | void GraphRevolve::setTranslate(int x,int y){ 8 | this->x = x; 9 | this->y = y; 10 | } 11 | void GraphRevolve::revolve(double re,QList &line, 12 | QList &circle, 13 | QList &rect, 14 | QList &triangle, 15 | QList &polygon){ 16 | QList new_line; 17 | QList list_rect; 18 | QList new_polygon; 19 | QLine _line; 20 | QRectF _rectf; 21 | QPolygonF _polygonf; 22 | matrix = QMatrix(); 23 | matrix.translate(x,y); 24 | matrix.rotate(re); 25 | matrix.translate(0-x,0-y); 26 | 27 | if(!line.isEmpty()){ 28 | for(int i=0; i< line.length();i++){ 29 | _line = line.at(i); 30 | _line = matrix.map(_line); 31 | new_line.append(_line); 32 | } 33 | line = new_line; 34 | } 35 | if(!circle.isEmpty()){ 36 | list_rect.clear(); 37 | for(int i=0; i< circle.length();i++){ 38 | _rectf = circle.at(i); 39 | QPointF qf = QPointF(_rectf.x(),_rectf.y()); 40 | int width = _rectf.height(); 41 | qf = matrix.map(qf); 42 | list_rect.append(QRectF(qf.x(),qf.y(),width,width)); 43 | } 44 | circle = list_rect; 45 | } 46 | // if(!oval.isEmpty()){ 47 | // list_rect.clear(); 48 | // for(int i=0; i< oval.length();i++){ 49 | // _rectf = oval.at(i); 50 | // _rectf = matrix.mapRect(_rectf); 51 | // list_rect.append(_rectf); 52 | // } 53 | // oval = list_rect; 54 | // } 55 | if(!triangle.isEmpty()){ 56 | triangleAddQPolygonF(triangle,polygon); 57 | triangle.clear(); 58 | } 59 | if(!rect.isEmpty()){ 60 | rectAddQPolygonF(rect,polygon); 61 | rect.clear(); 62 | } 63 | if(!polygon.isEmpty()){ 64 | for(int i=0; i< polygon.length();i++){ 65 | _polygonf = polygon.at(i); 66 | _polygonf = matrix.map(_polygonf); 67 | new_polygon.append(_polygonf); 68 | } 69 | polygon = new_polygon; 70 | } 71 | 72 | } 73 | 74 | void GraphRevolve::triangleAddQPolygonF(QList triangle,QList &polygon){ 75 | V_Triangle v; 76 | for(int i = 0;i rect,QList &polygon){ 86 | QRectF v; 87 | for(int i = 0;i 3 | #include 4 | #include 5 | 6 | CCohen_Sutherland::CCohen_Sutherland(V_Line rect) 7 | { 8 | this->rect = rect; 9 | } 10 | 11 | void CCohen_Sutherland::cutLine(QList &list_line){ 12 | QLine line; 13 | QList new_lines; 14 | V_Line v_line; 15 | for(int i = 0;i RectX2) 46 | { 47 | Temp = RectX2; 48 | RectX2 = RectX1; 49 | RectX1 = Temp; 50 | } 51 | if (RectY1 > RectY2) 52 | { 53 | Temp = RectY2; 54 | RectY2 = RectY1; 55 | RectY1 = Temp; 56 | } 57 | 58 | if (LineX1 < RectX1) 59 | { 60 | Code1 |= CLIP_CODE_W; 61 | } 62 | if (LineX1 > RectX2) 63 | { 64 | Code1 |= CLIP_CODE_E; 65 | } 66 | if (LineY1 < RectY1) 67 | { 68 | Code1 |= CLIP_CODE_N; 69 | } 70 | if (LineY1 > RectY2) 71 | { 72 | Code1 |= CLIP_CODE_S; 73 | } 74 | if (LineX2 < RectX1) 75 | { 76 | Code2 |= CLIP_CODE_W; 77 | } 78 | if (LineX2 > RectX2) 79 | { 80 | Code2 |= CLIP_CODE_E; 81 | } 82 | if (LineY2 < RectY1) 83 | { 84 | Code2 |= CLIP_CODE_N; 85 | } 86 | if (LineY2 > RectY2) 87 | { 88 | Code2 |= CLIP_CODE_S; 89 | } 90 | 91 | if (Code1 & Code2) 92 | { 93 | return 1; 94 | } 95 | if (Code1==0 && Code2==0) 96 | { 97 | return 0; 98 | } 99 | switch(Code1) 100 | { 101 | case CLIP_CODE_C: 102 | break; 103 | case CLIP_CODE_W: 104 | { 105 | LineX3 = RectX1; 106 | LineY3 = (LineX3-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 107 | break; 108 | } 109 | case CLIP_CODE_E: 110 | { 111 | LineX3 = RectX2; 112 | LineY3 = (LineX3-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 113 | break; 114 | } 115 | case CLIP_CODE_S: 116 | { 117 | LineY3 = RectY2; 118 | LineX3 = (LineY3-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 119 | break; 120 | } 121 | case CLIP_CODE_N: 122 | { 123 | LineY3 = RectY1; 124 | LineX3 = (LineY3-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 125 | break; 126 | } 127 | case CLIP_CODE_SW: 128 | { 129 | LineY3 = RectY2; 130 | LineX3 = (LineY3-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 131 | if (LineX3 < RectX1) 132 | { 133 | LineX3 = RectX1; 134 | LineY3 = (LineX3-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 135 | } 136 | break; 137 | } 138 | case CLIP_CODE_SE: 139 | { 140 | LineY3 = RectY2; 141 | LineX3 = (LineY3-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 142 | if (LineX3 > RectX2) 143 | { 144 | LineX3 = RectX2; 145 | LineY3 = (LineX3-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 146 | } 147 | break; 148 | } 149 | case CLIP_CODE_NW: 150 | { 151 | LineY3 = RectY1; 152 | LineX3 = (LineY3-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 153 | if (LineX3 < RectX1) 154 | { 155 | LineX3 = RectX1; 156 | LineY3 = (LineX3-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 157 | } 158 | break; 159 | } 160 | case CLIP_CODE_NE: 161 | { 162 | LineY3 = RectY1; 163 | LineX3 = (LineY3-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 164 | if (LineX3 > RectX2) 165 | { 166 | LineX3 = RectX2; 167 | LineY3 = (LineX3-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 168 | } 169 | break; 170 | } 171 | } 172 | 173 | switch(Code2) 174 | { 175 | case CLIP_CODE_C: 176 | break; 177 | case CLIP_CODE_W: 178 | { 179 | LineX4 = RectX1; 180 | LineY4 = (LineX4-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 181 | break; 182 | } 183 | case CLIP_CODE_E: 184 | { 185 | LineX4 = RectX2; 186 | LineY4 = (LineX4-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 187 | break; 188 | } 189 | case CLIP_CODE_S: 190 | { 191 | LineY4 = RectY2; 192 | LineX4 = (LineY4-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 193 | break; 194 | } 195 | case CLIP_CODE_N: 196 | { 197 | LineY4 = RectY1; 198 | LineX4 = (LineY4-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 199 | break; 200 | } 201 | case CLIP_CODE_SW: 202 | { 203 | LineY4 = RectY2; 204 | LineX4 = (LineY4-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 205 | if (LineX4 < RectX1) 206 | { 207 | LineX4 = RectX1; 208 | LineY4 = (LineX4-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 209 | } 210 | break; 211 | } 212 | case CLIP_CODE_SE: 213 | { 214 | LineY4 = RectY2; 215 | LineX4 = (LineY4-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 216 | if (LineX4 > RectX2) 217 | { 218 | LineX4 = RectX2; 219 | LineY4 = (LineX4-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 220 | } 221 | break; 222 | } 223 | case CLIP_CODE_NW: 224 | { 225 | LineY4 = RectY1; 226 | LineX4 = (LineY4-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 227 | if (LineX4 < RectX1) 228 | { 229 | LineX4 = RectX1; 230 | LineY4 = (LineX4-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 231 | } 232 | break; 233 | } 234 | case CLIP_CODE_NE: 235 | { 236 | LineY4 = RectY1; 237 | LineX4 = (LineY4-LineY1)*(LineX2-LineX1)/(LineY2-LineY1) + LineX1 + 0.5; 238 | if (LineX4 > RectX2) 239 | { 240 | LineX4 = RectX2; 241 | LineY4 = (LineX4-LineX1)*(LineY2-LineY1)/(LineX2-LineX1) + LineY1 + 0.5; 242 | } 243 | break; 244 | } 245 | } 246 | 247 | if (LineX3RectX2 248 | || LineX4RectX2 249 | || LineY3RectY2 250 | || LineX4RectY2) 251 | { 252 | return 1; 253 | } 254 | 255 | return 0; 256 | } 257 | -------------------------------------------------------------------------------- /mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include 5 | #include 6 | 7 | MainWindow::MainWindow(QWidget *parent) : 8 | QMainWindow(parent), 9 | ui(new Ui::MainWindow) 10 | { 11 | ui->setupUi(this); 12 | ui->opengl->hide(); 13 | ui->mainToolBar->init(); 14 | ui->widget->setMainWindow(this,ui->mainToolBar->getColor()); 15 | resize(800,600); 16 | status_action = new QLabel(); 17 | status_mouse = new QLabel(); 18 | statusBar()->addPermanentWidget(status_action,1); 19 | statusBar()->addPermanentWidget(status_mouse); 20 | setActionType(Normal); 21 | } 22 | 23 | MainWindow::~MainWindow() 24 | { 25 | delete status_action; 26 | delete status_mouse; 27 | delete ui; 28 | } 29 | 30 | void MainWindow::setActionType(ActionType type){ 31 | action_type = type; 32 | int v=0; 33 | QColor color; 34 | switch(type){ 35 | case Normal: 36 | status_action->setText(tr("Normal")); 37 | break; 38 | 39 | case D_Line: 40 | status_action->setText(tr("Draw Line")); 41 | break; 42 | 43 | case D_Circle: 44 | status_action->setText(tr("Draw Circle")); 45 | break; 46 | 47 | case D_Triangle: 48 | status_action->setText(tr("Draw Triangle")); 49 | break; 50 | 51 | case D_Polygon: 52 | status_action->setText(tr("Draw Polygon")); 53 | break; 54 | case D_Oval: 55 | status_action->setText(tr("Draw Oval")); 56 | break; 57 | case O_LineCut: 58 | status_action->setText(tr("Line Cut")); 59 | break; 60 | case O_Move: 61 | status_action->setText(tr("Move")); 62 | break; 63 | case O_Revolve: 64 | status_action->setText(tr("Revolve")); 65 | break; 66 | case O_Scaled: 67 | status_action->setText(tr("Scaled")); 68 | break; 69 | case M_Reset: 70 | status_action->setText(tr("Matrix Reset")); 71 | break; 72 | case M_Move: 73 | status_action->setText(tr("Matrix Move")); 74 | break; 75 | case M_Rotate: 76 | status_action->setText(tr("Matrix Rotate")); 77 | break; 78 | case M_Scaled: 79 | status_action->setText(tr("Matrix Scaled")); 80 | break; 81 | case E_Erase: 82 | if(ui->widget->bitmapCheck()){ 83 | status_action->setText(tr("Erase")); 84 | v = 5; 85 | ui->widget->setCircleR(v,v); 86 | ui->mainToolBar->setAction(E_Erase,&v); 87 | ui->mainToolBar->getColor()->setColor(QColor(0,0,0,0)); 88 | } 89 | break; 90 | case E_Fill: 91 | if(ui->widget->bitmapCheck()){ 92 | status_action->setText(tr("Fill")); 93 | // v=5; 94 | // ui->mainToolBar->setAction(E_Fill,&v); 95 | QColor c = ui->mainToolBar->getColor()->getColor(); 96 | c.setAlpha(255); 97 | ui->mainToolBar->getColor()->setColor(c); 98 | } 99 | break; 100 | case TD_Normal: 101 | status_action->setText(tr("3D Mode")); 102 | break; 103 | case TD_Cube: 104 | status_action->setText(tr("3D Cube")); 105 | break; 106 | case TD_Vertebral: 107 | status_action->setText(tr("3D Vertebral")); 108 | break; 109 | default: 110 | status_action->setText(tr("!!!!warning!!!! unknown action")); 111 | } 112 | if(is_3d){ 113 | ui->opengl->setActionType(type); 114 | }else{ 115 | ui->widget->setActionType(type); 116 | if(type==M_Reset){ 117 | setActionType(Normal); 118 | } 119 | } 120 | } 121 | 122 | ActionType MainWindow::getActionType(){ 123 | return action_type; 124 | } 125 | 126 | void MainWindow::resizeEvent(QResizeEvent * event) { 127 | QSize size = event->size(); 128 | QRect rect = QRect(1, 1, size.width() - 2, size.height() - ui->menuBar->height()-ui->mainToolBar->height() - ui->statusBar->height() - 2); 129 | if(!is_3d){ 130 | ui->widget->setGeometry(rect); 131 | }else{ 132 | ui->opengl->setGeometry(rect); 133 | } 134 | } 135 | 136 | void MainWindow::on_actionLine_triggered() 137 | { 138 | setActionType(D_Line); 139 | } 140 | 141 | void MainWindow::on_actionCircle_triggered() 142 | { 143 | setActionType(D_Circle); 144 | } 145 | 146 | void MainWindow::on_actionOval_triggered() 147 | { 148 | setActionType(D_Oval); 149 | } 150 | 151 | void MainWindow::on_actionPolygon_triggered() 152 | { 153 | setActionType(D_Polygon); 154 | } 155 | 156 | void MainWindow::on_actionTriangle_triggered() 157 | { 158 | setActionType(D_Triangle); 159 | } 160 | 161 | void MainWindow::on_actionClear_triggered() 162 | { 163 | ui->widget->clear(); 164 | } 165 | 166 | void MainWindow::on_actionRect_triggered() 167 | { 168 | setActionType(D_Rect); 169 | } 170 | 171 | void MainWindow::on_actionLineCut_triggered() 172 | { 173 | setActionType(O_LineCut); 174 | } 175 | 176 | void MainWindow::on_actionMove_triggered() 177 | { 178 | setActionType(O_Move); 179 | } 180 | 181 | void MainWindow::on_actionRevolve_triggered() 182 | { 183 | setActionType(O_Revolve); 184 | } 185 | 186 | void MainWindow::on_actionScaled_triggered() 187 | { 188 | setActionType(O_Scaled); 189 | } 190 | 191 | void MainWindow::on_actionSave_triggered() 192 | { 193 | QString file = QFileDialog::getSaveFileName(this, tr("Save File"), " ", tr("Image (*.png)")); 194 | if(!file.isEmpty()){ 195 | QImage image = QImage(this->ui->widget->width(),this->ui->widget->height(),QImage::Format_ARGB32); 196 | QPainter painter(&image); 197 | ui->widget->paint(painter,true); 198 | if(image.save(file,"png")){ 199 | QMessageBox::information(this, tr("Save success"), tr("Save success")+"\n"+file); 200 | }else{ 201 | QMessageBox::warning(this, tr("Save error"), tr("Save error\nPlease try again!")+"\n"+file); 202 | } 203 | } 204 | } 205 | 206 | void MainWindow::on_actionMReset_triggered() 207 | { 208 | setActionType(M_Reset); 209 | } 210 | 211 | void MainWindow::on_actionMScaled_triggered() 212 | { 213 | setActionType(M_Scaled); 214 | } 215 | 216 | void MainWindow::on_actionMRotate_triggered() 217 | { 218 | setActionType(M_Rotate); 219 | } 220 | 221 | void MainWindow::on_actionMMove_triggered() 222 | { 223 | setActionType(M_Move); 224 | } 225 | 226 | void MainWindow::on_actionToBitmap_triggered() 227 | { 228 | if(ui->widget->vectorToBitmap()){ 229 | //Set To Vector 230 | ui->actionToBitmap->setText(tr("ToVector")); 231 | }else{ 232 | //Set To Bitmap 233 | ui->actionToBitmap->setText(tr("ToBitmap")); 234 | setActionType(Normal); 235 | } 236 | } 237 | 238 | void MainWindow::on_actionErase_triggered() 239 | { 240 | setActionType(E_Erase); 241 | } 242 | 243 | void MainWindow::on_actionFill_triggered() 244 | { 245 | setActionType(E_Fill); 246 | } 247 | QWidget * MainWindow::getPainterWidget(){ 248 | return ui->widget; 249 | } 250 | 251 | void MainWindow::on_actionTo3D_triggered() 252 | { 253 | if(is_3d){ 254 | //to 2D 255 | ui->actionTo3D->setText(tr("To3D")); 256 | ui->opengl->hide(); 257 | ui->widget->show(); 258 | setActionType(Normal); 259 | }else{ 260 | //to 3D 261 | ui->actionTo3D->setText(tr("To2D")); 262 | ui->widget->hide(); 263 | ui->opengl->show(); 264 | setActionType(TD_Normal); 265 | } 266 | is_3d = !is_3d; 267 | QSize size(width(),height()); 268 | QResizeEvent sz(size,size); 269 | QApplication::sendEvent(this,&sz); 270 | } 271 | 272 | void MainWindow::on_actionCube_triggered() 273 | { 274 | if(checkIs3D()){ 275 | setActionType(TD_Cube); 276 | } 277 | } 278 | 279 | bool MainWindow::checkIs3D(){ 280 | if(!is_3d){ 281 | QMessageBox::warning(this,tr("Action error"),tr("Action error.\nMust swicth to 3D mode.")); 282 | } 283 | return is_3d; 284 | } 285 | 286 | void MainWindow::on_actionVertebral_triggered() 287 | { 288 | if(checkIs3D()){ 289 | setActionType(TD_Vertebral); 290 | } 291 | } 292 | 293 | void MainWindow::on_action3DRect_triggered() 294 | { 295 | if(checkIs3D()){ 296 | setActionType(TD_Normal); 297 | } 298 | } 299 | 300 | void MainWindow::on_actionAuthor_triggered() 301 | { 302 | QMessageBox::about(this, tr("About the author"), tr("loveyu\nhttp://www.loveyu.org/")); 303 | } 304 | 305 | void MainWindow::on_actionInfo_triggered() 306 | { 307 | QMessageBox::information(this, tr("About This Programe"), tr("A simple basic image rendering tool,\n supports several 3D presentations."), QMessageBox::Ok); 308 | } 309 | 310 | void MainWindow::on_actionExit_triggered() 311 | { 312 | close(); 313 | } 314 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 488 10 | 331 11 | 12 | 13 | 14 | Graph Edit 15 | 16 | 17 | 18 | 19 | 20 | 0 21 | 0 22 | 201 23 | 211 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | true 33 | 34 | 35 | 36 | 230 37 | 10 38 | 211 39 | 181 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 0 48 | 0 49 | 488 50 | 26 51 | 52 | 53 | 54 | 55 | File 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | Draw 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | GraphEdit 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | VectorEdit 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 3D 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | Transform 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | About 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | true 128 | 129 | 130 | false 131 | 132 | 133 | true 134 | 135 | 136 | TopToolBarArea 137 | 138 | 139 | false 140 | 141 | 142 | 143 | 144 | QStatusBar { border-top: 1px solid #aaa;} 145 | 146 | 147 | 148 | 149 | Save 150 | 151 | 152 | Ctrl+S 153 | 154 | 155 | 156 | 157 | Exit 158 | 159 | 160 | Ctrl+Q 161 | 162 | 163 | 164 | 165 | Line 166 | 167 | 168 | 169 | 170 | Circle 171 | 172 | 173 | 174 | 175 | Oval 176 | 177 | 178 | 179 | 180 | Line Cut 181 | 182 | 183 | Line Cut 184 | 185 | 186 | 187 | 188 | Fill 189 | 190 | 191 | 192 | 193 | Revolve 194 | 195 | 196 | 197 | 198 | Polygon 199 | 200 | 201 | 202 | 203 | Scaled 204 | 205 | 206 | 207 | 208 | Cube 209 | 210 | 211 | 212 | 213 | Triangle 214 | 215 | 216 | 217 | 218 | Move 219 | 220 | 221 | 222 | 223 | Erase 224 | 225 | 226 | 227 | 228 | Clear 229 | 230 | 231 | Ctrl+C 232 | 233 | 234 | 235 | 236 | Rect 237 | 238 | 239 | 240 | 241 | Move 242 | 243 | 244 | 245 | 246 | Rotate 247 | 248 | 249 | 250 | 251 | Scaled 252 | 253 | 254 | 255 | 256 | Reset 257 | 258 | 259 | 260 | 261 | ToBitmap 262 | 263 | 264 | 265 | 266 | To3D 267 | 268 | 269 | 270 | 271 | Vertebral 272 | 273 | 274 | 275 | 276 | Rect 277 | 278 | 279 | 280 | 281 | Author 282 | 283 | 284 | 285 | 286 | Info 287 | 288 | 289 | 290 | 291 | 292 | 293 | PainterWidget 294 | QWidget 295 |
painterwidget.h
296 | 1 297 |
298 | 299 | GraphTool 300 | QToolBar 301 |
graphtool.h
302 |
303 | 304 | MyOpenGL 305 | QOpenGLWidget 306 |
myopengl.h
307 |
308 |
309 | 310 | 311 |
312 | -------------------------------------------------------------------------------- /zh_CN.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | GraphTool 6 | 7 | 8 | Radius: 9 | 边框: 10 | 11 | 12 | 13 | Radius 14 | 边框 15 | 16 | 17 | 18 | MainWindow 19 | 20 | 21 | Graph Edit 22 | 图像编辑 23 | 24 | 25 | 26 | File 27 | 文件 28 | 29 | 30 | 31 | Draw 32 | 绘图 33 | 34 | 35 | 36 | GraphEdit 37 | 图像编辑 38 | 39 | 40 | 41 | VectorEdit 42 | 向量编辑 43 | 44 | 45 | 46 | 3D 47 | 3D 48 | 49 | 50 | 51 | Transform 52 | 变换 53 | 54 | 55 | 56 | About 57 | 关于 58 | 59 | 60 | 61 | Save 62 | 保存 63 | 64 | 65 | 66 | Exit 67 | 退出 68 | 69 | 70 | 71 | Line 72 | 线 73 | 74 | 75 | 76 | Circle 77 | 78 | 79 | 80 | 81 | Oval 82 | 椭圆 83 | 84 | 85 | 86 | 87 | 88 | Line Cut 89 | 线段裁剪 90 | 91 | 92 | 93 | 94 | Fill 95 | 填充 96 | 97 | 98 | 99 | 100 | Revolve 101 | 旋转 102 | 103 | 104 | 105 | Polygon 106 | 多边形 107 | 108 | 109 | 110 | 111 | 112 | Scaled 113 | 缩放 114 | 115 | 116 | 117 | Cube 118 | 正方体 119 | 120 | 121 | 122 | Triangle 123 | 三角形 124 | 125 | 126 | 127 | 128 | 129 | Move 130 | 移动 131 | 132 | 133 | 134 | 135 | Erase 136 | 擦除 137 | 138 | 139 | 140 | Clear 141 | 清除 142 | 143 | 144 | 145 | 146 | Rect 147 | 矩形 148 | 149 | 150 | 151 | Rotate 152 | 旋转 153 | 154 | 155 | 156 | Reset 157 | 重置 158 | 159 | 160 | 161 | 162 | ToBitmap 163 | 转为位图 164 | 165 | 166 | 167 | 168 | To3D 169 | 转为3D 170 | 171 | 172 | 173 | Vertebral 174 | 椎体 175 | 176 | 177 | 178 | Author 179 | 作者 180 | 181 | 182 | 183 | Info 184 | 信息 185 | 186 | 187 | 188 | Normal 189 | 正常 190 | 191 | 192 | 193 | Draw Line 194 | 画线 195 | 196 | 197 | 198 | Draw Circle 199 | 画圆 200 | 201 | 202 | 203 | Draw Triangle 204 | 画三角形 205 | 206 | 207 | 208 | Draw Polygon 209 | 画多边形 210 | 211 | 212 | 213 | Draw Oval 214 | 画椭圆 215 | 216 | 217 | 218 | Matrix Reset 219 | 矩阵重置 220 | 221 | 222 | 223 | Matrix Move 224 | 矩阵移动 225 | 226 | 227 | 228 | Matrix Rotate 229 | 矩阵旋转 230 | 231 | 232 | 233 | Matrix Scaled 234 | 矩阵缩放 235 | 236 | 237 | 238 | 3D Mode 239 | 3D模式 240 | 241 | 242 | 243 | 3D Cube 244 | 3D正方体 245 | 246 | 247 | 248 | !!!!warning!!!! unknown action 249 | !警告!未知操作 250 | 251 | 252 | 253 | Save File 254 | 保存文件 255 | 256 | 257 | 258 | Image (*.png) 259 | 260 | 261 | 262 | 263 | Save success 264 | 保存成功 265 | 266 | 267 | 268 | Save error 269 | 保存错误 270 | 271 | 272 | 273 | Save error 274 | Please try again! 275 | 保存错误 276 | 请重试一次! 277 | 278 | 279 | 280 | ToVector 281 | 转为向量 282 | 283 | 284 | 285 | To2D 286 | 转为2D 287 | 288 | 289 | 290 | Action error 291 | 操作错误 292 | 293 | 294 | 295 | Action error. 296 | Must swicth to 3D mode. 297 | 操作错误。 298 | 必须先转换为3D模式。 299 | 300 | 301 | 302 | About the author 303 | 关于作者 304 | 305 | 306 | 307 | loveyu 308 | http://www.loveyu.org/ 309 | 310 | 311 | 312 | 313 | About This Programe 314 | 关于程序 315 | 316 | 317 | 318 | A simple basic image rendering tool, 319 | supports several 3D presentations. 320 | 一个简单的基本图像绘制工具, 321 | 支持几种3D的演示。 322 | 323 | 324 | 325 | PainterWidget 326 | 327 | 328 | Action stop 329 | 操作停止 330 | 331 | 332 | 333 | You must convert to bitmap image. 334 | 你必须转为位图再操作。 335 | 336 | 337 | 338 | -------------------------------------------------------------------------------- /painterwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "painterwidget.h" 2 | #include "ccohen_sutherland.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "imageaction.h" 8 | #include 9 | 10 | PainterWidget::PainterWidget(QWidget *parent) : 11 | QWidget(parent) 12 | { 13 | setMouseTracking(true); 14 | setFocusPolicy(Qt::StrongFocus); 15 | //init pen 16 | dash_pen.setBrush(QBrush(qRgb(100,100,100))); 17 | QVector dashes; 18 | qreal space = 3; 19 | dashes << 5 << space << 5 <accept(); 71 | QPainter painter(this); 72 | ActionType type = main_window->getActionType(); 73 | if(image_flag && image_out.width()>0){ 74 | if(matrix_flag){ 75 | painter.setMatrix(matrix); 76 | } 77 | painter.drawImage(QRectF(0,0,image_out.width(),image_out.height()),image_out); 78 | }else{ 79 | paint(painter); 80 | } 81 | painter.setMatrixEnabled(false); 82 | switch(type){ 83 | case Normal: 84 | case O_LineCut: 85 | //Draw normal Rect 86 | if(normal_rect.x1>=0 && normal_rect.x2>=0 && normal_rect.y1>=0 && normal_rect.y2>=0 87 | && (normal_rect.x1!=normal_rect.x2 || normal_rect.y1!=normal_rect.y2)){ 88 | painter.setPen(dash_pen); 89 | painter.drawRect(normal_rect.x2,normal_rect.y2,normal_rect.x1-normal_rect.x2,normal_rect.y1-normal_rect.y2); 90 | } 91 | break; 92 | case D_Line: 93 | if(line.x1>=0 && line.x2>=0 && line.y1>=0 && line.y2>=0 94 | && (line.x1!=line.x2 || line.y1!=line.y2)){ 95 | painter.drawLine(line.x1,line.y1,line.x2,line.y2); 96 | } 97 | break; 98 | case D_Rect: 99 | if(line.x1>=0 && line.x2>=0 && line.y1>=0 && line.y2>=0 100 | && (line.x1!=line.x2 || line.y1!=line.y2)){ 101 | painter.drawRect(line.x1,line.y1,line.x2-line.x1,line.y2-line.y1); 102 | } 103 | break; 104 | case D_Circle: 105 | if(circle.r!=0){ 106 | painter.drawEllipse(circle.x-circle.r/2,circle.y-circle.r/2,circle.r,circle.r); 107 | } 108 | break; 109 | case D_Oval: 110 | if(circle.r!=0 || circle.r2!=0){ 111 | painter.drawEllipse(circle.x-circle.r/2,circle.y-circle.r2/2,circle.r,circle.r2); 112 | } 113 | break; 114 | case D_Triangle: 115 | switch(triangle_count){ 116 | case 1: 117 | if(triangle.x1>=0 && triangle.x2>=1){ 118 | painter.drawLine(triangle.x1,triangle.y1,triangle.x2,triangle.y2); 119 | } 120 | break; 121 | case 2: 122 | if(triangle.x1>=0 && triangle.x2>=0 && triangle.x3>=0){ 123 | painter.drawLine(triangle.x1,triangle.y1,triangle.x2,triangle.y2); 124 | painter.setPen(dash_pen); 125 | painter.drawLine(triangle.x2,triangle.y2,triangle.x3,triangle.y3); 126 | painter.drawLine(triangle.x1,triangle.y1,triangle.x3,triangle.y3); 127 | } 128 | break; 129 | } 130 | break; 131 | case D_Polygon: 132 | if(polygon.length()>1){ 133 | //Draw Polygon 134 | painter.drawPolyline(polygon); 135 | } 136 | if(line.x1>=0 && line.x2>=0){ 137 | painter.setPen(dash_pen); 138 | painter.drawLine(line.x1,line.y1,line.x2,line.y2); 139 | if(triangle_count==-1){ 140 | painter.drawLine(QPoint(line.x2,line.y2),polygon.at(0)); 141 | } 142 | } 143 | break; 144 | case O_Revolve: 145 | case O_Scaled: 146 | case M_Rotate: 147 | case M_Scaled: 148 | if(circle.x>=0&& circle.y>=0){ 149 | painter.drawEllipse(circle.x-2,circle.y-2,4,4); 150 | } 151 | break; 152 | case E_Erase: 153 | if(circle.x>=0&& circle.y>=0 && circle.r>0){ 154 | if(press_status){ 155 | painter.fillRect(QRect(circle.x-circle.r,circle.y-circle.r,circle.r*2,circle.r*2),color_label->getColor()); 156 | }else{ 157 | painter.drawRect(circle.x-circle.r,circle.y-circle.r,circle.r*2,circle.r*2); 158 | } 159 | } 160 | break; 161 | default: 162 | break; 163 | } 164 | } 165 | void PainterWidget::wheelEvent(QWheelEvent *event){ 166 | int numSteps = event->delta() / 8 / 15; 167 | event->accept(); 168 | switch(main_window->getActionType()){ 169 | case O_Revolve: 170 | if(graph_revolve && circle.x>=0){ 171 | graph_revolve->revolve(0-numSteps,list_line,list_circle,list_rect,list_triangle,list_polygon); 172 | update(true); 173 | } 174 | break; 175 | case O_Scaled: 176 | if(graph_scaled && circle.x>=0){ 177 | graph_scaled->scaled(0-numSteps,list_line,list_oval,list_circle,list_rect,list_triangle,list_polygon); 178 | update(true); 179 | } 180 | break; 181 | case M_Rotate: 182 | if(circle.x>=0){ 183 | matrix_value+=numSteps; 184 | matrix = matrix_backup; 185 | matrix.translate(circle.x,circle.y); 186 | matrix.rotate(matrix_value); 187 | matrix.translate(0-circle.x,0-circle.y); 188 | update(true); 189 | } 190 | break; 191 | 192 | case M_Scaled: 193 | if(circle.x>=0){ 194 | matrix_value*=numSteps>0?1.25:0.9; 195 | matrix = matrix_backup; 196 | matrix.translate(circle.x,circle.y); 197 | matrix.scale(matrix_value,matrix_value); 198 | matrix.translate(0-circle.x,0-circle.y); 199 | update(true); 200 | } 201 | break; 202 | default: 203 | break; 204 | } 205 | } 206 | void PainterWidget::mousePressEvent(QMouseEvent *event){ 207 | int x = event->x(); 208 | int y = event->y(); 209 | press_status = true; 210 | switch(main_window->getActionType()){ 211 | case Normal: 212 | case O_LineCut: 213 | normal_rect.x1 = x; 214 | normal_rect.y1 = y; 215 | break; 216 | case D_Line: 217 | case D_Rect: 218 | line.x1 = x; 219 | line.y1 = y; 220 | break; 221 | case D_Circle: 222 | case D_Oval: 223 | circle.x = x; 224 | circle.y = y; 225 | circle.r = circle.r2 = 0; 226 | break; 227 | case D_Triangle: 228 | switch(triangle_count){ 229 | case 0: 230 | triangle.x1 = x; 231 | triangle.y1 = y; 232 | triangle_count++; 233 | break; 234 | case 1: 235 | triangle_count++; 236 | break; 237 | case 2: 238 | triangle_count=0; 239 | list_triangle.append(triangle); 240 | triangle = {-1,-1,-1,-1,-1,-1}; 241 | break; 242 | } 243 | break; 244 | case D_Polygon: 245 | line.x1 = x; 246 | line.y1 = y; 247 | polygon.append(QPointF(x,y)); 248 | if(triangle_count==-1){ 249 | //ADD 250 | list_polygon.append(polygon); 251 | update(true); 252 | polygon.clear(); 253 | line.x1=-1; 254 | line.x2=-1; 255 | } 256 | triangle_count++; 257 | break; 258 | case O_Move: 259 | setCursor(Qt::ClosedHandCursor); 260 | graph_move = new GraphMove(x,y); 261 | break; 262 | case M_Move: 263 | line.x1 = x; 264 | line.y1 = y; 265 | matrix_backup = matrix; 266 | setCursor(Qt::ClosedHandCursor); 267 | break; 268 | case O_Revolve: 269 | graph_revolve->setTranslate(x,y); 270 | circle.x = x; 271 | circle.y = y; 272 | break; 273 | case O_Scaled: 274 | graph_scaled->setTranslate(x,y); 275 | circle.x = x; 276 | circle.y = y; 277 | break; 278 | case M_Rotate: 279 | case M_Scaled: 280 | matrix_backup = matrix; 281 | circle.x = x; 282 | circle.y = y; 283 | break; 284 | case E_Erase: 285 | ImageAction::delete_rect(QRect(circle.x-circle.r,circle.y-circle.r,circle.r*2,circle.r*2),image_out,color_label->getColor()); 286 | update(); 287 | break; 288 | case E_Fill: 289 | ImageAction::fill(x,y,image_out,color_label->getColor()); 290 | update(); 291 | break; 292 | default: 293 | break; 294 | } 295 | } 296 | void PainterWidget::mouseReleaseEvent(QMouseEvent *event){ 297 | event->accept(); 298 | press_status = false; 299 | switch(main_window->getActionType()){ 300 | case Normal: 301 | normal_rect.x1 = -1; 302 | normal_rect.y1 = -1; 303 | break; 304 | case D_Line: 305 | list_line.append(QLine(line.x1,line.y1,line.x2,line.y2)); 306 | line.x1 = -1; 307 | line.x2 = -1; 308 | break; 309 | case D_Rect: 310 | list_rect.append(QRectF(line.x1,line.y1,line.x2-line.x1,line.y2-line.y1)); 311 | line.x1 = -1; 312 | line.x2 = -1; 313 | break; 314 | case D_Circle: 315 | list_circle.append(QRectF(circle.x,circle.y,circle.r,circle.r)); 316 | circle.r = 0; 317 | break; 318 | case D_Oval: 319 | list_oval.append(QRectF(circle.x,circle.y,circle.r,circle.r2)); 320 | circle.r =circle.r2=0; 321 | break; 322 | case O_LineCut: 323 | //Line Cut 324 | line_cs = new CCohen_Sutherland((normal_rect)); 325 | line_cs->cutLine(list_line); 326 | delete line_cs; 327 | //reset 328 | normal_rect.x1 = -1; 329 | normal_rect.y1 = -1; 330 | break; 331 | case O_Move: 332 | setCursor(Qt::ArrowCursor); 333 | delete graph_move; 334 | break; 335 | case M_Move: 336 | setCursor(Qt::ArrowCursor); 337 | matrix_backup = matrix; 338 | break; 339 | default: 340 | break; 341 | } 342 | update(true); 343 | } 344 | void PainterWidget::mouseMoveEvent(QMouseEvent *event){ 345 | int x = event->x(); 346 | int y = event->y(); 347 | ActionType type = main_window->getActionType(); 348 | main_window->status_mouse->setText(QString("%1:%2").arg(x).arg(y)); 349 | if(!press_status){ 350 | if(type!=E_Erase && (triangle_count==0 || (type!=D_Triangle && type!=D_Polygon))){ 351 | return; 352 | } 353 | } 354 | switch(type){ 355 | case Normal: 356 | case O_LineCut: 357 | normal_rect.x2 = x; 358 | normal_rect.y2 = y; 359 | break; 360 | case D_Line: 361 | case D_Polygon: 362 | line.x2 = x; 363 | line.y2 = y; 364 | break; 365 | case D_Rect: 366 | line.x2 = x; 367 | line.y2 = y; 368 | if(press_shift){ 369 | //width=height 370 | if(abs(line.x2-line.x1)0){ 373 | if(line.y1>y){ 374 | line.y2 = line.y1-x; 375 | }else{ 376 | line.y2 = line.y1+x; 377 | } 378 | }else{ 379 | if(line.y1>y){ 380 | line.y2 = line.y1+x; 381 | }else{ 382 | line.y2 = line.y1-x; 383 | } 384 | } 385 | }else{ 386 | y = y-line.y1; 387 | if(y>0){ 388 | if(line.x1>x){ 389 | line.x2 = line.x1-y; 390 | }else{ 391 | line.x2 = line.x1+y; 392 | } 393 | }else{ 394 | if(line.x1>x){ 395 | line.x2 = line.x1+y; 396 | }else{ 397 | line.x2 = line.x1-y; 398 | } 399 | } 400 | } 401 | } 402 | break; 403 | case D_Circle: 404 | circle.r = sqrt((double)(x-circle.x)*(x-circle.x)*2)*2; 405 | break; 406 | case D_Oval: 407 | circle.r = sqrt((double)(x-circle.x)*(x-circle.x)*2)*2; 408 | circle.r2 = (y-circle.y)*2; 409 | break; 410 | case D_Triangle: 411 | switch(triangle_count){ 412 | case 1: 413 | triangle.x2 = x; 414 | triangle.y2 = y; 415 | break; 416 | case 2: 417 | triangle.x3 = x; 418 | triangle.y3 = y; 419 | break; 420 | } 421 | break; 422 | case O_Move: 423 | if(graph_move->is_move(x,y)){ 424 | graph_move->move(list_line,list_circle,list_oval,list_rect,list_triangle,list_polygon); 425 | } 426 | break; 427 | case M_Move: 428 | matrix = matrix_backup; 429 | matrix.translate(x-line.x1,y-line.y1); 430 | break; 431 | case E_Erase: 432 | circle.x = x; 433 | circle.y = y; 434 | if(press_status){ 435 | ImageAction::delete_rect(QRect(circle.x-circle.r,circle.y-circle.r,circle.r*2,circle.r*2),image_out,color_label->getColor()); 436 | } 437 | break; 438 | default: 439 | break; 440 | } 441 | if(type==O_Move){ 442 | update(true); 443 | }else{ 444 | update(); 445 | } 446 | } 447 | void PainterWidget::keyPressEvent(QKeyEvent *event){ 448 | if(event->key()==Qt::Key_Shift){ 449 | press_shift = true; 450 | } 451 | if(event->key()==Qt::Key_Escape && main_window->getActionType()==D_Polygon){ 452 | triangle_count=-1; 453 | } 454 | } 455 | void PainterWidget::keyReleaseEvent(QKeyEvent *event){ 456 | if(event->key()==Qt::Key_Shift){ 457 | press_shift = false; 458 | } 459 | } 460 | void PainterWidget::clear(){ 461 | list_line.clear(); 462 | list_circle.clear(); 463 | list_oval.clear(); 464 | list_rect.clear(); 465 | list_triangle.clear(); 466 | list_polygon.clear(); 467 | update(); 468 | } 469 | bool PainterWidget::vectorToBitmap(){ 470 | if(!image_flag){ 471 | //now is vector 472 | image_out = QImage(width(),height(),QImage::Format_ARGB32); 473 | QPainter painter(&image_out); 474 | paint(painter); 475 | clear(); 476 | }else{ 477 | //now is bitmap 478 | image_out = QImage(); 479 | update(); 480 | } 481 | image_flag = !image_flag; 482 | return image_flag; 483 | } 484 | void PainterWidget::update(){ 485 | QWidget::update(); 486 | } 487 | void PainterWidget::update(bool up_img){ 488 | if(image_flag && up_img){ 489 | QPainter painter(&image_out); 490 | paint(painter); 491 | clear(); 492 | } 493 | QWidget::update(); 494 | } 495 | void PainterWidget::setActionType(ActionType type){ 496 | if(graph_revolve!=NULL){ 497 | delete graph_revolve; 498 | graph_revolve = NULL; 499 | } 500 | if(graph_scaled!=NULL){ 501 | delete graph_scaled; 502 | graph_scaled = NULL; 503 | } 504 | switch(type){ 505 | case D_Line: 506 | line = {0,0,0,0}; 507 | break; 508 | case D_Triangle: 509 | triangle = {-1,-1,-1,-1,-1,-1}; 510 | case D_Polygon: 511 | polygon.clear(); 512 | triangle_count = 0; 513 | break; 514 | case O_Revolve: 515 | circle = {-1,-1,-1,-1}; 516 | graph_revolve = new GraphRevolve(); 517 | break; 518 | case O_Scaled: 519 | circle = {-1,-1,-1,-1}; 520 | graph_scaled = new GraphScaled(); 521 | break; 522 | case M_Reset: 523 | matrix_flag = false; 524 | matrix=matrix_backup = QMatrix(); 525 | update(); 526 | break; 527 | case M_Move: 528 | case M_Rotate: 529 | matrix_value = 0; 530 | matrix_flag = true; 531 | circle = {-1,-1,-1,-1}; 532 | update(); 533 | break; 534 | case M_Scaled: 535 | matrix_value = 1; 536 | matrix_flag = true; 537 | circle = {-1,-1,-1,-1}; 538 | update(); 539 | break; 540 | default: 541 | break; 542 | } 543 | line.x1=-1; 544 | line.x2=-1; 545 | } 546 | 547 | void PainterWidget::setCircleR(int r,int r2){ 548 | switch (main_window->getActionType()) { 549 | case E_Erase: 550 | circle.r = r; 551 | circle.r2 = r2; 552 | break; 553 | default: 554 | break; 555 | } 556 | } 557 | -------------------------------------------------------------------------------- /Graph.pro.user.3.3-pre1: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {98755ab4-adc4-4e6e-b106-e8ce11192296} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.3 MSVC2013 OpenGL 64bit 62 | Desktop Qt 5.3 MSVC2013 OpenGL 64bit 63 | qt.53.win64_msvc2013_64_opengl_kit 64 | 0 65 | 0 66 | 0 67 | 68 | G:/QT/build-Graph-Desktop_Qt_5_3_MSVC2013_OpenGL_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | false 76 | true 77 | 78 | false 79 | 80 | 81 | true 82 | Make 83 | 84 | Qt4ProjectManager.MakeStep 85 | 86 | false 87 | 88 | 89 | 90 | 2 91 | 构建 92 | 93 | ProjectExplorer.BuildSteps.Build 94 | 95 | 96 | 97 | true 98 | Make 99 | 100 | Qt4ProjectManager.MakeStep 101 | 102 | true 103 | clean 104 | 105 | 106 | 1 107 | 清理 108 | 109 | ProjectExplorer.BuildSteps.Clean 110 | 111 | 2 112 | false 113 | 114 | Debug 115 | 116 | Qt4ProjectManager.Qt4BuildConfiguration 117 | 2 118 | true 119 | 120 | 121 | G:/QT/build-Graph-Desktop_Qt_5_3_MSVC2013_OpenGL_64bit-Release 122 | 123 | 124 | true 125 | qmake 126 | 127 | QtProjectManager.QMakeBuildStep 128 | false 129 | true 130 | 131 | false 132 | 133 | 134 | true 135 | Make 136 | 137 | Qt4ProjectManager.MakeStep 138 | 139 | false 140 | 141 | 142 | 143 | 2 144 | 构建 145 | 146 | ProjectExplorer.BuildSteps.Build 147 | 148 | 149 | 150 | true 151 | Make 152 | 153 | Qt4ProjectManager.MakeStep 154 | 155 | true 156 | clean 157 | 158 | 159 | 1 160 | 清理 161 | 162 | ProjectExplorer.BuildSteps.Clean 163 | 164 | 2 165 | false 166 | 167 | Release 168 | 169 | Qt4ProjectManager.Qt4BuildConfiguration 170 | 0 171 | true 172 | 173 | 2 174 | 175 | 176 | 0 177 | 部署 178 | 179 | ProjectExplorer.BuildSteps.Deploy 180 | 181 | 1 182 | 在本地部署 183 | 184 | ProjectExplorer.DefaultDeployConfiguration 185 | 186 | 1 187 | 188 | 189 | 190 | false 191 | false 192 | false 193 | false 194 | true 195 | 0.01 196 | 10 197 | true 198 | 1 199 | 25 200 | 201 | 1 202 | true 203 | false 204 | true 205 | valgrind 206 | 207 | 0 208 | 1 209 | 2 210 | 3 211 | 4 212 | 5 213 | 6 214 | 7 215 | 8 216 | 9 217 | 10 218 | 11 219 | 12 220 | 13 221 | 14 222 | 223 | 2 224 | 225 | Graph 226 | 227 | Qt4ProjectManager.Qt4RunConfiguration:G:/QT/Graph/Graph.pro 228 | 229 | Graph.pro 230 | false 231 | false 232 | 233 | 3768 234 | false 235 | true 236 | false 237 | false 238 | true 239 | 240 | 1 241 | 242 | 243 | 244 | ProjectExplorer.Project.TargetCount 245 | 1 246 | 247 | 248 | ProjectExplorer.Project.Updater.FileVersion 249 | 16 250 | 251 | 252 | Version 253 | 16 254 | 255 | 256 | -------------------------------------------------------------------------------- /Graph.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {98755ab4-adc4-4e6e-b106-e8ce11192296} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.4.0 MinGW 32bit 62 | Desktop Qt 5.4.0 MinGW 32bit 63 | qt.54.win32_mingw491_kit 64 | 1 65 | 0 66 | 0 67 | 68 | G:/QT/build-Graph-Desktop_Qt_5_4_0_MinGW_32bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | false 76 | true 77 | 78 | false 79 | false 80 | 81 | 82 | true 83 | Make 84 | 85 | Qt4ProjectManager.MakeStep 86 | 87 | false 88 | 89 | 90 | 91 | 2 92 | 构建 93 | 94 | ProjectExplorer.BuildSteps.Build 95 | 96 | 97 | 98 | true 99 | Make 100 | 101 | Qt4ProjectManager.MakeStep 102 | 103 | true 104 | clean 105 | 106 | 107 | 1 108 | 清理 109 | 110 | ProjectExplorer.BuildSteps.Clean 111 | 112 | 2 113 | false 114 | 115 | Debug 116 | 117 | Qt4ProjectManager.Qt4BuildConfiguration 118 | 2 119 | true 120 | 121 | 122 | G:/QT/build-Graph-Desktop_Qt_5_4_0_MinGW_32bit-Release 123 | 124 | 125 | true 126 | qmake 127 | 128 | QtProjectManager.QMakeBuildStep 129 | false 130 | true 131 | 132 | false 133 | false 134 | 135 | 136 | true 137 | Make 138 | 139 | Qt4ProjectManager.MakeStep 140 | 141 | false 142 | 143 | 144 | 145 | 2 146 | 构建 147 | 148 | ProjectExplorer.BuildSteps.Build 149 | 150 | 151 | 152 | true 153 | Make 154 | 155 | Qt4ProjectManager.MakeStep 156 | 157 | true 158 | clean 159 | 160 | 161 | 1 162 | 清理 163 | 164 | ProjectExplorer.BuildSteps.Clean 165 | 166 | 2 167 | false 168 | 169 | Release 170 | 171 | Qt4ProjectManager.Qt4BuildConfiguration 172 | 0 173 | true 174 | 175 | 2 176 | 177 | 178 | 0 179 | 部署 180 | 181 | ProjectExplorer.BuildSteps.Deploy 182 | 183 | 1 184 | 在本地部署 185 | 186 | ProjectExplorer.DefaultDeployConfiguration 187 | 188 | 1 189 | 190 | 191 | 192 | false 193 | false 194 | false 195 | false 196 | true 197 | 0.01 198 | 10 199 | true 200 | 1 201 | 25 202 | 203 | 1 204 | true 205 | false 206 | true 207 | valgrind 208 | 209 | 0 210 | 1 211 | 2 212 | 3 213 | 4 214 | 5 215 | 6 216 | 7 217 | 8 218 | 9 219 | 10 220 | 11 221 | 12 222 | 13 223 | 14 224 | 225 | 2 226 | 227 | Graph 228 | 229 | Qt4ProjectManager.Qt4RunConfiguration:G:/QT/Graph/Graph.pro 230 | 231 | Graph.pro 232 | false 233 | false 234 | 235 | 3768 236 | false 237 | true 238 | false 239 | false 240 | true 241 | 242 | 1 243 | 244 | 245 | 246 | ProjectExplorer.Project.TargetCount 247 | 1 248 | 249 | 250 | ProjectExplorer.Project.Updater.FileVersion 251 | 18 252 | 253 | 254 | Version 255 | 18 256 | 257 | 258 | --------------------------------------------------------------------------------