File indexing completed on 2024-04-28 04:32:07
0001 /* 0002 * Copyright (C) 2010-2015 by Stephen Allewell 0003 * steve.allewell@gmail.com 0004 * 0005 * This program is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation; either version 2 of the License, or 0008 * (at your option) any later version. 0009 */ 0010 0011 #include "Scale.h" 0012 0013 #include <QPainter> 0014 #include <QScrollBar> 0015 0016 #include "configuration.h" 0017 0018 Scale::Scale(Qt::Orientation orientation) 0019 : QWidget() 0020 { 0021 setContextMenuPolicy(Qt::ActionsContextMenu); 0022 0023 if (orientation == Qt::Horizontal) { 0024 setContentsMargins(3, 0, 0, 0); 0025 setMinimumHeight(Configuration::editor_HorizontalScaleHeight()); 0026 } else { 0027 setContentsMargins(0, 3, 0, 0); 0028 setMinimumWidth(Configuration::editor_VerticalScaleWidth()); 0029 } 0030 0031 m_orientation = orientation; 0032 m_units = Configuration::editor_FormatScalesAs(); 0033 m_cellSize = 0; 0034 m_cellCount = 0; 0035 m_offset = 0; 0036 } 0037 0038 void Scale::setUnits(Configuration::EnumEditor_FormatScalesAs::type units) 0039 { 0040 m_units = units; 0041 update(); 0042 } 0043 0044 void Scale::setCellSize(double cellSize) 0045 { 0046 m_cellSize = cellSize; 0047 update(); 0048 } 0049 0050 void Scale::setCellGrouping(int cellGrouping) 0051 { 0052 m_cellGrouping = cellGrouping; 0053 update(); 0054 } 0055 0056 void Scale::setCellCount(int cellCount) 0057 { 0058 m_cellCount = cellCount; 0059 update(); 0060 } 0061 0062 void Scale::setClothCount(double clothCount) 0063 { 0064 m_clothCount = clothCount; 0065 update(); 0066 } 0067 0068 void Scale::setClothCountUnits(Configuration::EnumEditor_ClothCountUnits::type clothCountUnits) 0069 { 0070 m_clothCountUnits = clothCountUnits; 0071 update(); 0072 } 0073 0074 void Scale::setOffset(double offset) 0075 { 0076 m_offset = offset; 0077 update(); 0078 } 0079 0080 QSize Scale::sizeHint() const 0081 { 0082 if (m_orientation == Qt::Horizontal) { 0083 return QSize(500, 30); 0084 } else { 0085 return QSize(30, 500); 0086 } 0087 } 0088 0089 void Scale::paintEvent(QPaintEvent *) 0090 { 0091 double length = m_cellSize * m_cellCount; 0092 double halfLength = length / 2; 0093 int width = contentsRect().width(); 0094 int height = contentsRect().height(); 0095 int left = contentsRect().left(); 0096 int right = contentsRect().right(); 0097 int bottom = contentsRect().bottom(); 0098 int top = contentsRect().top(); 0099 bool clothCountUnitsInches = (m_clothCountUnits == Configuration::EnumEditor_ClothCountUnits::Inches); 0100 0101 // Default to Stitches 0102 // subtick should be 1 cell 0103 double subTick = m_cellSize; 0104 int minorTicks = 1; 0105 int majorTicks = m_cellGrouping; 0106 0107 int ticklen; 0108 0109 int textValue = 0; 0110 int textValueIncrement = m_cellGrouping; 0111 0112 switch (m_units) { 0113 case Configuration::EnumEditor_FormatScalesAs::Stitches: 0114 // Set to default above 0115 break; 0116 0117 case Configuration::EnumEditor_FormatScalesAs::Centimeters: 0118 // subtick should be 1/10 CM 0119 subTick = m_cellSize * m_clothCount / (clothCountUnitsInches ? 25.4 : 10); 0120 minorTicks = 5; 0121 majorTicks = 10; 0122 textValueIncrement = 1; 0123 break; 0124 0125 case Configuration::EnumEditor_FormatScalesAs::Inches: 0126 // subtick should be 1/16 inch 0127 subTick = m_cellSize * m_clothCount / (clothCountUnitsInches ? 16 : 6.299); 0128 majorTicks = 16; 0129 minorTicks = 4; 0130 textValueIncrement = 1; 0131 break; 0132 0133 default: 0134 break; 0135 } 0136 0137 QPainter painter; 0138 painter.begin(this); 0139 painter.setBrush(Qt::black); 0140 0141 QPolygonF midPoint; 0142 0143 if (m_orientation == Qt::Horizontal) { 0144 painter.drawLine(left, bottom, right, bottom); 0145 midPoint << QPointF(left + halfLength + m_offset, bottom) << QPointF(left + halfLength - 5 + m_offset, bottom - 5) 0146 << QPointF(left + halfLength + 5 + m_offset, bottom - 5); 0147 } else { 0148 painter.drawLine(right, top, right, bottom); 0149 midPoint << QPointF(right, top + halfLength + m_offset) << QPointF(right - 5, top + halfLength - 5 + m_offset) 0150 << QPointF(right - 5, top + halfLength + 5 + m_offset); 0151 } 0152 0153 painter.drawPolygon(midPoint); 0154 0155 int ticks = length / subTick; 0156 0157 for (int i = 0; i <= ticks; i++) { 0158 ticklen = 3; 0159 double tickwidth = i * subTick; 0160 0161 if ((i % minorTicks) == 0) { 0162 ticklen = 6; 0163 } 0164 0165 if ((i % majorTicks) == 0) { 0166 ticklen = 9; 0167 } 0168 0169 if (m_orientation == Qt::Horizontal) { 0170 painter.drawLine(QPointF(left + tickwidth + m_offset, bottom), QPointF(left + tickwidth + m_offset, bottom - ticklen)); 0171 } else { 0172 painter.drawLine(QPointF(right, top + tickwidth + m_offset), QPointF(right - ticklen, top + tickwidth + m_offset)); 0173 } 0174 0175 if ((i % majorTicks) == 0) { 0176 if (m_orientation == Qt::Horizontal) { 0177 painter.drawText(QPointF(left + tickwidth + m_offset + 1, bottom - ticklen), QString::fromLatin1("%1").arg(textValue)); 0178 } else { 0179 painter.drawText(QRectF(left, top + tickwidth + m_offset + 1, width, height), Qt::AlignTop, QString::fromLatin1("%1").arg(textValue)); 0180 } 0181 0182 textValue += textValueIncrement; 0183 } 0184 } 0185 0186 painter.end(); 0187 } 0188 0189 #include "moc_Scale.cpp"