|
@@ -31,7 +31,7 @@ ImageView::ImageView(QWidget* parent) :
|
|
|
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
|
|
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
- //������ͼ����
|
|
|
+ //设置视图属性
|
|
|
//setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
setRenderHint(QPainter::Antialiasing);
|
|
|
setMouseTracking(true);
|
|
@@ -53,9 +53,9 @@ void ImageView::setPixmap(const QPixmap& newPixmap) {
|
|
|
scene()->setSceneRect(sceneRect);
|
|
|
setSceneRect(scene()->itemsBoundingRect());
|
|
|
this->resize(m_pixmap.width(), m_pixmap.height());
|
|
|
- m_imageOffset = QPoint(0, 0); // ����ͼƬƫ����Ϊ(0, 0)
|
|
|
+ m_imageOffset = QPoint(0, 0);
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
- update(); // �����ػ�
|
|
|
+ update();
|
|
|
}
|
|
|
|
|
|
void ImageView::setCurPixmap(const QPixmap& newPixmap) {
|
|
@@ -70,9 +70,9 @@ void ImageView::setCurPixmap(const QPixmap& newPixmap) {
|
|
|
scene()->setSceneRect(sceneRect);
|
|
|
setSceneRect(scene()->itemsBoundingRect());
|
|
|
//this->resize(m_pixmap.width(), m_pixmap.height());
|
|
|
- m_imageOffset = QPoint(0, 0); // ����ͼƬƫ����Ϊ(0, 0)
|
|
|
+ m_imageOffset = QPoint(0, 0);
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
- update(); // �����ػ�
|
|
|
+ update();
|
|
|
}
|
|
|
else {
|
|
|
this->m_pixmap = newPixmap;
|
|
@@ -100,7 +100,7 @@ void ImageView::paintEvent(QPaintEvent* event) {
|
|
|
|
|
|
void ImageView::mousePressEvent(QMouseEvent* event) {
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
- // ��������û�� item ʱ�������ƶ�
|
|
|
+
|
|
|
QPointF point = mapToScene(event->pos());
|
|
|
if (scene()->itemAt(point, transform()) != nullptr
|
|
|
&& scene()->itemAt(point, transform())->type() == QGraphicsPixmapItem::Type) {
|
|
@@ -115,7 +115,7 @@ void ImageView::mousePressEvent(QMouseEvent* event) {
|
|
|
|
|
|
if (m_bRulerState) {
|
|
|
if (!m_isCreating) {
|
|
|
- // ��һ�ε������ʼ����
|
|
|
+
|
|
|
m_isCreating = true;
|
|
|
m_startPos = mapToScene(event->pos());
|
|
|
m_pTempLine = scene()->addLine(QLineF(m_startPos, m_startPos),
|
|
@@ -123,7 +123,7 @@ void ImageView::mousePressEvent(QMouseEvent* event) {
|
|
|
m_pTempLine->setZValue(2);
|
|
|
}
|
|
|
else {
|
|
|
- // �ڶ��ε������ɴ���
|
|
|
+
|
|
|
m_isCreating = false;
|
|
|
QPointF endPos = mapToScene(event->pos());
|
|
|
scene()->removeItem(m_pTempLine);
|
|
@@ -147,17 +147,17 @@ void ImageView::mouseMoveEvent(QMouseEvent* event) {
|
|
|
}
|
|
|
|
|
|
if (m_bMouseTranslate) {
|
|
|
- // ������굱ǰλ�����ϴ�λ�õIJ�ֵ
|
|
|
+
|
|
|
QPointF delta = event->pos() - m_lastMousePos;
|
|
|
- // ƽ����ͼ
|
|
|
+
|
|
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
|
|
|
verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y());
|
|
|
- m_lastMousePos = event->pos(); // ��������
|
|
|
+ m_lastMousePos = event->pos();
|
|
|
}
|
|
|
else if ((event->buttons() & Qt::RightButton) && m_isDrawing && m_rightButtonPressed) {
|
|
|
if (!m_curDrawing && (event->pos() - m_rightClickPressPos).manhattanLength() > 5) {
|
|
|
m_curDrawing = true;
|
|
|
- // ���ݳ�ʼλ�ÿ�ʼ����·��
|
|
|
+
|
|
|
QPointF scenePos = mapToScene(m_rightClickPressPos);
|
|
|
m_currentPath = QPainterPath(scenePos);
|
|
|
m_currentPathItem = new QGraphicsPathItem();
|
|
@@ -214,7 +214,7 @@ void ImageView::mouseReleaseEvent(QMouseEvent* event) {
|
|
|
void ImageView::wheelEvent(QWheelEvent* event)
|
|
|
{
|
|
|
if (event->orientation() == Qt::Vertical) {
|
|
|
- event->ignore(); // ������ֱ�����¼��������ù�����������
|
|
|
+ event->ignore();
|
|
|
return;
|
|
|
}
|
|
|
event->accept();
|
|
@@ -223,13 +223,13 @@ void ImageView::wheelEvent(QWheelEvent* event)
|
|
|
void ImageView::resizeEvent(QResizeEvent* event)
|
|
|
{
|
|
|
QGraphicsView::resizeEvent(event);
|
|
|
- viewport()->update(); // ���ڴ�С�ı�ʱ�ػ�ʮ����
|
|
|
+ viewport()->update();
|
|
|
}
|
|
|
|
|
|
void ImageView::scrollContentsBy(int dx, int dy)
|
|
|
{
|
|
|
QGraphicsView::scrollContentsBy(dx, dy);
|
|
|
- viewport()->update(); // ����ʱ����ʮ����
|
|
|
+ viewport()->update();
|
|
|
}
|
|
|
|
|
|
void ImageView::drawCrosshair(QPainter& painter)
|
|
@@ -238,67 +238,67 @@ void ImageView::drawCrosshair(QPainter& painter)
|
|
|
int resolutionWidth = viewRect.width();
|
|
|
int resolutionHeight = viewRect.height();
|
|
|
painter.setPen(QPen(QColor(139, 105, 20, 255), 1));
|
|
|
- // ���ƴ�ֱ��
|
|
|
+
|
|
|
painter.drawLine(resolutionWidth / 2, 0, resolutionWidth / 2, resolutionHeight);
|
|
|
- // ����ˮƽ��
|
|
|
+
|
|
|
painter.drawLine(0, resolutionHeight / 2, resolutionWidth, resolutionHeight / 2);
|
|
|
|
|
|
int centerX = resolutionWidth / 2;
|
|
|
int centerY = resolutionHeight / 2;
|
|
|
|
|
|
- int rulerLength = viewRect.width(); // ���߳���
|
|
|
- int tickSpacing = 5; // �̶ȼ��
|
|
|
+ int rulerLength = viewRect.width();
|
|
|
+ int tickSpacing = 5;
|
|
|
|
|
|
- // ����ˮƽ�������ϵ�����
|
|
|
+
|
|
|
int rulerStartXHorizontal = centerX;
|
|
|
int rulerStartYHorizontal = centerY;
|
|
|
|
|
|
for (int i = 0; i <= rulerLength / 2; i += tickSpacing) {
|
|
|
int tickHeight = 0;
|
|
|
- if (i % (tickSpacing * 10) == 0) { // ���̶�
|
|
|
- tickHeight = 15;
|
|
|
+ if (i % (tickSpacing * 10) == 0) {
|
|
|
+ tickHeight = 30;
|
|
|
}
|
|
|
- else if (i % (tickSpacing * 5) == 0) { // �еȿ̶�
|
|
|
- tickHeight = 10;
|
|
|
+ else if (i % (tickSpacing * 5) == 0) {
|
|
|
+ tickHeight = 15;
|
|
|
}
|
|
|
- else { // �̶̿�
|
|
|
+ else {
|
|
|
tickHeight = 5;
|
|
|
}
|
|
|
|
|
|
- painter.drawLine(rulerStartXHorizontal + i, rulerStartYHorizontal, rulerStartXHorizontal + i, rulerStartYHorizontal - tickHeight); // ���ƿ̶���
|
|
|
- painter.drawLine(rulerStartXHorizontal - i, rulerStartYHorizontal, rulerStartXHorizontal - i, rulerStartYHorizontal - tickHeight); // ���ƿ̶���
|
|
|
+ painter.drawLine(rulerStartXHorizontal + i, rulerStartYHorizontal + tickHeight, rulerStartXHorizontal + i, rulerStartYHorizontal - tickHeight);
|
|
|
+ painter.drawLine(rulerStartXHorizontal - i, rulerStartYHorizontal + tickHeight, rulerStartXHorizontal - i, rulerStartYHorizontal - tickHeight);
|
|
|
}
|
|
|
|
|
|
- // ���ƴ�ֱ�������ϵ�����
|
|
|
+
|
|
|
int rulerStartXVertical = centerX;
|
|
|
int rulerStartYVertical = centerY;
|
|
|
|
|
|
for (int i = 0; i <= rulerLength / 2; i += tickSpacing) {
|
|
|
int tickWidth = 0;
|
|
|
- if (i % (tickSpacing * 10) == 0) { // ���̶�
|
|
|
+ if (i % (tickSpacing * 10) == 0) {
|
|
|
tickWidth = 15;
|
|
|
}
|
|
|
- else if (i % (tickSpacing * 5) == 0) { // �еȿ̶�
|
|
|
+ else if (i % (tickSpacing * 5) == 0) {
|
|
|
tickWidth = 10;
|
|
|
}
|
|
|
- else { // �̶̿�
|
|
|
+ else {
|
|
|
tickWidth = 5;
|
|
|
}
|
|
|
- painter.drawLine(rulerStartXVertical, rulerStartYVertical - i, rulerStartXVertical - tickWidth, rulerStartYVertical - i);
|
|
|
- painter.drawLine(rulerStartXVertical, rulerStartYVertical + i, rulerStartXVertical - tickWidth, rulerStartYVertical + i);
|
|
|
+ painter.drawLine(rulerStartXVertical+ tickWidth, rulerStartYVertical - i, rulerStartXVertical - tickWidth, rulerStartYVertical - i);
|
|
|
+ painter.drawLine(rulerStartXVertical + tickWidth, rulerStartYVertical + i, rulerStartXVertical - tickWidth, rulerStartYVertical + i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void ImageView::saveImage()
|
|
|
{
|
|
|
- // ʹ�õ�ǰʱ������Ĭ���ļ���
|
|
|
+
|
|
|
QString defaultFileName = QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm-ss") + ".png";
|
|
|
|
|
|
QString fileName = QFileDialog::getSaveFileName(
|
|
|
this,
|
|
|
- tr("����ͼƬ"),
|
|
|
- QDir::homePath() + "/" + defaultFileName, // Ĭ��·��Ϊ�û�Ŀ¼���ļ���Ϊ��ǰʱ��
|
|
|
- tr("PNG �ļ� (*.png);;JPEG �ļ� (*.jpg);;�����ļ� (*)")
|
|
|
+ tr("save picture"),
|
|
|
+ QDir::homePath() + "/" + defaultFileName,
|
|
|
+ tr("PNG (*.png);;JPEG (*.jpg);; (*)")
|
|
|
);
|
|
|
if (!fileName.isEmpty()) {
|
|
|
QImage image(viewport()->size(), QImage::Format_ARGB32);
|
|
@@ -306,10 +306,10 @@ void ImageView::saveImage()
|
|
|
QPainter painter(&image);
|
|
|
this->render(&painter);
|
|
|
if (!image.save(fileName)) {
|
|
|
- QMessageBox::warning(this, tr("����ʧ��"), tr("������ͼƬ��ָ��·����"));
|
|
|
+ QMessageBox::warning(this,tr("save failed"),tr("The picture cannot be saved to the specified path"));
|
|
|
}
|
|
|
else {
|
|
|
- QMessageBox::information(this, tr("����ɹ�"), tr("ͼƬ�ѳɹ����浽��") + fileName);
|
|
|
+ QMessageBox::information(this, tr("save sucessful"),tr("The picture has been successfully saved to:") + fileName);
|
|
|
}
|
|
|
painter.end();
|
|
|
}
|