File indexing completed on 2024-05-19 04:36:39
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2013-2014 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "Ruler.h" 0021 0022 #include <tikz/core/Document.h> 0023 0024 #include <QDebug> 0025 #include <QPainterPath> 0026 0027 namespace tikz { 0028 namespace ui { 0029 0030 static const int s_ruler_size = 16; 0031 0032 Ruler::Ruler(Qt::Orientation orientation, QWidget* parent) 0033 : QWidget(parent) 0034 , m_orientation(orientation) 0035 , m_origin(0.0) 0036 , m_unit(tikz::Unit::Centimeter) 0037 , m_zoom(1.0) 0038 { 0039 QFont txtFont("Sans Serif Mono", 7); 0040 txtFont.setStyleHint(QFont::TypeWriter, QFont::PreferOutline); 0041 setFont(txtFont); 0042 0043 setMouseTracking(true); 0044 } 0045 0046 QSize Ruler::minimumSizeHint() const 0047 { 0048 return QSize(s_ruler_size, s_ruler_size); 0049 } 0050 0051 Qt::Orientation Ruler::orientation() const 0052 { 0053 return m_orientation; 0054 } 0055 0056 qreal Ruler::origin() const 0057 { 0058 return m_origin; 0059 } 0060 0061 tikz::Unit Ruler::unit() const 0062 { 0063 return m_unit; 0064 } 0065 0066 qreal Ruler::zoom() const 0067 { 0068 return m_zoom; 0069 } 0070 0071 void Ruler::setOrigin(qreal origin) 0072 { 0073 if (m_origin != origin) { 0074 m_origin = origin; 0075 update(); 0076 } 0077 } 0078 0079 void Ruler::setUnit(tikz::Unit unit) 0080 { 0081 if (m_unit != unit) { 0082 m_unit = unit; 0083 update(); 0084 } 0085 } 0086 0087 void Ruler::setZoom(qreal zoom) 0088 { 0089 if (m_zoom != zoom) 0090 { 0091 m_zoom = zoom; 0092 update(); 0093 } 0094 } 0095 0096 void Ruler::setMousePos(const QPoint & cursorPos) 0097 { 0098 m_mousePos = mapFromGlobal(cursorPos); 0099 update(); 0100 } 0101 0102 void Ruler::mouseMoveEvent(QMouseEvent* event) 0103 { 0104 QWidget::mouseMoveEvent(event); 0105 m_mousePos = event->pos(); 0106 update(); 0107 } 0108 0109 void Ruler::paintEvent(QPaintEvent* event) 0110 { 0111 QPainter painter(this); 0112 painter.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing); 0113 painter.setPen(Qt::black); 0114 0115 // translate origin 0116 const bool horizontal = Qt::Horizontal == m_orientation; 0117 painter.translate(horizontal ? m_origin : 0, horizontal ? 0 : m_origin); 0118 0119 // how many pixels is one unit ? 0120 const qreal dpi = physicalDpi(); 0121 const qreal pixelPerUnit = m_zoom * dpi / 1.0_in .convertTo(m_unit).value(); 0122 0123 if (horizontal) { 0124 for (int i = floor(-m_origin / pixelPerUnit); i < (width() - m_origin) / pixelPerUnit; ++i) { 0125 QPointF start(i * pixelPerUnit, 3.0); 0126 QPointF end(i * pixelPerUnit, height()); 0127 painter.drawLine(start, end); 0128 0129 painter.drawText(start + QPointF(1, (horizontal ? 7 : -2)), 0130 QString::number(int(1) * i)); 0131 } 0132 } else { 0133 for (int i = floor(-m_origin / pixelPerUnit); i < (height() - m_origin) / pixelPerUnit; ++i) { 0134 QPointF start(3.0, i * pixelPerUnit); 0135 QPointF end(width(), i * pixelPerUnit); 0136 painter.drawLine(start, end); 0137 0138 painter.drawText(start + QPointF(1, (horizontal ? 7 : -2)), 0139 QString::number(int(-1) * i)); 0140 } 0141 } 0142 0143 painter.resetTransform(); 0144 drawMouseTick(&painter); 0145 } 0146 0147 void Ruler::drawMouseTick(QPainter* painter) 0148 { 0149 QPainterPath triangle; 0150 if (Qt::Horizontal == m_orientation) { 0151 triangle.moveTo(QPoint(m_mousePos.x(), rect().bottom())); 0152 triangle.lineTo(QPoint(m_mousePos.x() + 4, rect().bottom() - 4)); 0153 triangle.lineTo(QPoint(m_mousePos.x() - 4, rect().bottom() - 4)); 0154 triangle.closeSubpath(); 0155 } else { 0156 triangle.moveTo(QPoint(rect().right(), m_mousePos.y())); 0157 triangle.lineTo(QPoint(rect().right() - 4, m_mousePos.y() - 4)); 0158 triangle.lineTo(QPoint(rect().right() - 4, m_mousePos.y() + 4)); 0159 triangle.closeSubpath(); 0160 } 0161 painter->fillPath(triangle, Qt::black); 0162 } 0163 0164 qreal Ruler::physicalDpi() const 0165 { 0166 return (m_orientation == Qt::Horizontal) ? physicalDpiX() : physicalDpiY(); 0167 } 0168 0169 } 0170 } 0171 0172 // kate: indent-width 4; replace-tabs on;