📄 mygraphic.cpp
字号:
/************************************************************************************************************************************************************** **** equal III the graphic builder **** **** Copyright (C) 2003 Oleksiy Pylypenko **** **** This file may be distributed and/or modified under the terms of the **** GNU General Public License version 2 as published by the Free Software **** Foundation and appearing in the file license included in the **** packaging of this file. **** **** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE **** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. **** **** Contact earthman@inbox.ru if any conditions of this licensing are **** not clear to you. **** **** ********************************************************************************* *****************************************************************************/#include <qmultilineedit.h>#include <qlayout.h>#include <qpainter.h>#include <qtooltip.h>#include <qregexp.h>#include "mygraphic.h"class GraphicToolTip : public QToolTip{public: GraphicToolTip(QWidget * parent) : QToolTip(parent) { } void maybeTip( const QPoint &pos ) { if(!parentWidget()->inherits("myGraphic")) return; QString s = ((myGraphic*)parentWidget())->getGraphicName(pos); if (s.isNull()) return; QRect r(pos.x(),pos.y(),pos.x()+10,pos.y()+10); tip( r, s ); }};myGraphic::myGraphic( QWidget *parent) : QWidget( parent ){ setMinimumSize(100,100); setMouseTracking(true); new GraphicToolTip(this); clear();}void myGraphic::clear(void){ myWidth = 0; myHeight = 0; myImage = 0;}myGraphic::~myGraphic(){ UpdateAnImage(); }QSize myGraphic::sizeHint(void) const{ return QSize(600,600);}void myGraphic::resizeEvent( QResizeEvent * event ){ updateSize(event->size() - QSize(2,2));}void myGraphic::paintEvent( QPaintEvent * /* event */){ QSize s = size();/* if(s.width() < s.height()){ s.setHeight(s.width()); }else{ s.setWidth(s.height()); }*/ imageMutex.lock(); QPainter p; p.begin( this ); if(anImage.isNull()){ p.fillRect(0,0,s.width(),s.height(),QBrush(QColor(255,255,255))); p.drawRect(0,0,s.width(),s.height()); }else{ if(s.width() != myWidth || s.height() != myHeight) {/* QPixmap pm; pm.convertFromImage(anImage,OrderedDither); QWMatrix m; m.scale((double)s.width()/myWidth, (double)s.height()/myHeight); pm = pm.xForm(m); p.drawPixmap(QPoint(1,1),pm); setBackgroundPixmap(pm);*/ p.fillRect(0,0,s.width(),s.height(),QBrush(QColor(255,255,255))); p.drawText(1,1,s.width()-2,s.height()-2, AlignHCenter | AlignVCenter ,tr("Need rebuild. Wait...")); }else{// setBackgroundPixmap p.drawImage(QPoint(1,1),anImage); } p.drawRect(0,0,s.width(),s.height()); } p.end(); imageMutex.unlock();}void myGraphic::UpdateAnImage(int w,int h,unsigned int *image,vector<QRgb> graphiccolor,vector<string> graphicname){ imageMutex.lock(); if(image){ if(myWidth * myHeight != w * h){ if(myImage)delete myImage; myImage = new unsigned int[w*h]; } myWidth = w; myHeight = h; memcpy(myImage,image,w*h*sizeof(unsigned int)); graphicNames = graphicname; anImage = QImage(w,h,32,0,QImage::IgnoreEndian); for(int y = 0;y < h;y++){ unsigned int *to = (unsigned int *)anImage.scanLine(y); unsigned int *from = image + y * w; for(int x = 0;x < w;x++) *to++ = graphiccolor[*from++]; } }else{ if(myImage)delete myImage; myWidth = 0; myHeight = 0; anImage = QImage(); graphicNames.clear(); } imageMutex.unlock(); repaint();}QString myGraphic::getGraphicName(QPoint pt){ pt = pt - QPoint(1,1); //QSize s = size() - QSize(2,2); // int index = anImage.pixelIndex(pt.x(),pt.y());// anImage.size(); QString r; int x = pt.x(),y = pt.y(); if(x < myWidth && y < myHeight && x>=1 && y>=1){ GraphicBuilder::DoubleRect from = {-1,-1,myWidth,myHeight},to = scale; double pa2mc_xc = (to.left - to.right) / (from.left - from.right); double pa2mc_yc = (to.top - to.bottom) / (from.top - from.bottom); double mathx = to.left - pa2mc_xc / 2. + double(x) * pa2mc_xc; double mathy = to.top - pa2mc_yc / 2. + double(y) * pa2mc_yc; r.sprintf("%.3f %.3f\n%s",mathx,mathy,graphicNames[myImage[x+y*myWidth]].c_str()); return r; } //r.sprintf("%d %d",pt.x(),pt.y()); return "";}#include <qfiledialog.h>#include <qmessagebox.h>void myGraphic::export_bitmap(void){// update(); QString fn = QFileDialog::getSaveFileName( QString::null, "Bitmap (*.bmp)", this ); if ( !fn.isEmpty() ){ if(fn.find(QRegExp("\\.[^\\.\\\\]*$"))==-1) fn += ".bmp"; QSize s = size(); s -= QSize(2,2); QPixmap pixmap(s); QPainter p; p.begin( &pixmap ); if(anImage.isNull()){ p.fillRect(0,0,s.width(),s.height(),QBrush(QColor(255,255,255))); }else{ if(s.width() != myWidth-2 || s.height() != myHeight-2) { QMessageBox::warning(this,tr("Wait for rebuild"),tr("Need rebuild. Wait... Then call export once more")); return; }else{ p.drawImage(QPoint(0,0),anImage); } } p.end(); if(!pixmap.save(fn,"BMP")) QMessageBox::warning(this,tr("Failed to save bitmap"),fn); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -