#include "../tool/tool.h"
#include "mainwindow.h"
#include <QtGui>
#include <QPainter>
#include <QMouseEvent>
#include <QtDebug>
MainWindow::MainWindow()
:paintArea(new PaintArea)
,scrollArea(new QScrollArea)
,m_basePen(0)
{
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(paintArea);
setCentralWidget(scrollArea);
setWindowTitle(tr("Drawing"));
createActions();
createMenus();
QDir pluginsDir("../plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
QFileInfo pluginFileInfo(fileName);
if (pluginFileInfo.completeSuffix() == "dll"
|| pluginFileInfo.completeSuffix() == "so")
{
QPluginLoader * pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *pluginObject = pluginLoader->instance();
if (pluginObject)
{
m_pluginLoader.push_back(pluginLoader);
m_pluginObject.push_back(pluginObject);
m_name.push_back(pluginsDir.absoluteFilePath(fileName));
QAction *newTool = new QAction(fileName.remove(".dll"), this);
m_toolAction.push_back(newTool);
connect(newTool, SIGNAL(triggered()), this, SLOT(chooseTool()));
toolMenu->addAction(newTool);
}
}
}
}
void MainWindow::open()//file菜单的open选项
{
const QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"),
QDir::currentPath());
if (!fileName.isEmpty()) {
if (!paintArea->openImage(fileName)) {
QMessageBox::information(this, tr("Plug & Paint"),
tr("Cannot load %1.").arg(fileName));
return;
}
paintArea->adjustSize();
}
}
bool MainWindow::saveAs()//file菜单的save选项
{
const QString initialPath = QDir::currentPath() + "/untitled.png";
const QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
initialPath);
if (fileName.isEmpty()) {
return false;
} else {
return paintArea->saveImage(fileName, "png");
}
}
void MainWindow::about() //说明
{
QMessageBox::about(this, tr("About Tools"),
tr("<b>Tools</b>"));
}
void MainWindow::createActions()
{
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAsAct = new QAction(tr("&Save As..."), this);
saveAsAct->setShortcuts(QKeySequence::SaveAs);
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
cutAct = new QAction(tr("&Cut"), this);
connect(cutAct, SIGNAL(triggered()), paintArea, SLOT(cut()));
copyAct = new QAction(tr("&Copy"), this);
connect(copyAct, SIGNAL(triggered()), paintArea, SLOT(copy()));
pasteAct = new QAction(tr("&Paste"), this);
connect(pasteAct, SIGNAL(triggered()), paintArea, SLOT(paste()));
deleteAct = new QAction(tr("&Delete"), this);
connect(deleteAct, SIGNAL(triggered()), paintArea, SLOT(Delete()));
aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}
void MainWindow::createMenus() //创建菜单
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator(); //增加分隔符
fileMenu->addAction(exitAct);
toolMenu = menuBar()->addMenu(tr("&Tool"));
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
editMenu->addAction(deleteAct);
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
}
void MainWindow::chooseTool()
{
int i;
for (i = 0; i < m_toolAction.size(); i++)
{
if (m_toolAction.at(i) == QObject::sender())
{
break;
}
}
m_basePen = qobject_cast<Producter *>(m_pluginObject.at(i));
paintArea->setTool(m_basePen);
paintArea->setPlugin(m_pluginLoader.at(i));
paintArea->setPath(m_name.at(i));
/*paintArea->update();*/
}
PaintArea::PaintArea(QWidget *parent)
:QWidget(parent)
,theImage(500, 400, QImage::Format_RGB32)
,m_basePen(0)
,m_producter(0)
,m_bMove(false)
,m_bResize(false)
,m_MouseDown(false)
,m_bPaste(false)
{
setAttribute(Qt::WA_StaticContents);
setAttribute(Qt::WA_NoBackground);
setAttribute(Qt::WA_MouseTracking);
theImage.fill(qRgb(255, 255, 255));
m_copy = new QAction(tr("copy") , this);
m_cut = new QAction(tr("cut") , this) ;
m_paste = new QAction(tr("paste") , this );
m_delete = new QAction(tr("delete") , this );
connect(m_copy , SIGNAL(triggered()) , this , SLOT(copy()));
connect(m_cut , SIGNAL(triggered()) , this , SLOT(cut()));
connect(m_paste , SIGNAL(triggered()) , this , SLOT(mousePaste()));
connect(m_delete , SIGNAL(triggered()) , this , SLOT(Delete()));
}
bool PaintArea::openImage(const QString &fileName)
{
QImage image;
if (!image.load(fileName))
return false;
setImage(image);
return true;
}
bool PaintArea::saveImage(const QString &fileName, const char *fileFormat)
{
return theImage.save(fileName, fileFormat);
}
void PaintArea::setImage(const QImage &image)
{
theImage = image.convertToFormat(QImage::Format_RGB32);
update();
updateGeometry();
}
QSize PaintArea::sizeHint() const
{
return theImage.size();
}
void PaintArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), theImage);
painter.setPen(Qt::black);
for (int i = 0; i < m_totalTool.size(); i++)
{
painter.drawPath(m_totalTool.at(i)->toolShape());
}
if (!m_basePen)
{
return;
}
painter.setPen(Qt::red);
painter.drawPath(m_basePen->toolShape());
update();
}
void PaintArea::mousePressEvent(QMouseEvent *event)
{
if (!m_producter)
{
return;
}
if (event->button() == Qt::RightButton)
{
m_mousePosition = event->pos();
return;
}
for (int i = 0; i < m_totalTool.size(); i++)
{
if (m_totalTool.at(i)->mousePressEvent(event))
{
m_basePen = m_totalTool.at(i);
m_number = i;
QWidget::mousePressEvent(event);
return;
}
}
ToolClass *tool = m_producter->creatTool();
m_totalTool.push_back(tool);
m_number = m_totalTool.size() - 1;
m_basePen = tool;
m_basePen->mousePressEvent(event);
QWidget::mousePressEvent(event);
}
void PaintArea::mouseMoveEvent(QMouseEvent *event)
{
if (!m_basePen)
{
return;
}
m_basePen->mouseMoveEvent(event);
QWidget::mouseMoveEvent(event);
update();
}
void PaintArea::mouseReleaseEvent(QMouseEvent *event)
{
if (!m_basePen)
{
return;
}
m_basePen->mouseReleaseEvent(event);
QWidget::mouseReleaseEvent(event);
/* update();*/
}
void PaintArea::setTool(Producter *pen)
{
m_producter = pen;
}
void PaintArea::setPlugin(QPluginLoader *plu)
{
m_pluginLoader = plu;
}
void PaintArea::setPath(QString name)
{
m_name = name;
}
void PaintArea::cut()
{
if (!m_basePen)
{
return;
}
m_bPaste = true;
/*update();*/
}
void PaintArea::copy()
{
if (!m_basePen)
{
return;
}
m_bPaste = true;
//m_bflag = true;
ToolClass *tool = m_basePen->getProductor()->creatTool();
QVector<QPoint> pp = m_basePen->getPoints();
tool->setPoints(m_basePen->getPoints());
m_totalTool.push_back(tool);
}
void PaintArea::mousePaste()
{
//m_bflag = false;
m_bPaste = false;
QVector<QPoint> points = m_basePen->getPoints();
QVector<QPoint> newPoints;
QPoint difference = m_mousePosition - points.at(0);
newPoin
没有合适的资源?快使用搜索试试~ 我知道了~
QT 插件系统和图形系统
共17个文件
h:6个
cpp:6个
pro:5个
5星 · 超过95%的资源 需积分: 31 32 下载量 41 浏览量
2013-04-16
15:12:04
上传
评论
收藏 11KB RAR 举报
温馨提示
一个qt做的插件系统和图形系统,里面的每种图形都弄成插件形式,所提供的图形也比较全,可以改变图形大小和移动。是在qwidget上做的。
资源推荐
资源详情
资源评论
收起资源包目录
tool.rar (17个子文件)
tool
rect
rect.cpp 3KB
rect.pro 422B
rect.h 823B
line
line.cpp 2KB
rect.pro 422B
line.h 748B
ellipse
ellipse.pro 433B
ellipse.h 783B
ellipse.cpp 4KB
plugins
temp
circle
circle.cpp 2KB
rect.pro 428B
circle.h 765B
tool
tool.H 701B
main
main.cpp 214B
mainwindow.cpp 9KB
mainwindow.h 2KB
tool.pro 427B
共 17 条
- 1
资源评论
- phoenices2013-12-08对于初学者的我很有用,谢谢
- 沉默圣者2015-03-13不错 功能挺全的
syzzzzzz
- 粉丝: 8
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功