File indexing completed on 2024-05-19 04:36:39

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013 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 "ArrowComboBox.h"
0021 
0022 #include "AbstractArrow.h"
0023 #include "IconComboBox.h"
0024 
0025 #include <tikz/core/Style.h>
0026 #include <tikz/core/tikz.h>
0027 
0028 #include <QStylePainter>
0029 #include <QHBoxLayout>
0030 #include <QStyle>
0031 #include <QStyleOptionGraphicsItem>
0032 #include <QAbstractItemView>
0033 #include <QDebug>
0034 
0035 #include <math.h>
0036 
0037 class ArrowComboBoxPrivate
0038 {
0039     public:
0040         ArrowComboBox * q = nullptr;
0041 
0042         bool arrowHead = true;
0043         QComboBox * comboBox = nullptr;
0044 
0045         void fillComboBox();
0046 };
0047 
0048 void ArrowComboBoxPrivate::fillComboBox()
0049 {
0050     QStyleOptionGraphicsItem option;
0051     option.initFrom(q);
0052 
0053     // get default icon size
0054     const int iconHeight = q->style()->pixelMetric(QStyle::PM_SmallIconSize, &option, q);
0055     const qreal w = q->physicalDpiX() / tikz::in2mm(1); // = pixel per mm
0056     const qreal h = q->physicalDpiY() / tikz::in2mm(1); // = pixel per mm
0057 
0058     // adjust desired icon size to avoid scaling
0059     const QSize iconSize(5 * w, iconHeight); // 10mm * w pixel per mm == [pixel] width
0060     comboBox->setIconSize(iconSize);
0061 
0062     // create horizontal edge from 0.1cm to 0.9cm
0063     tikz::core::Style style;
0064     style.setLineWidth(tikz::Value::veryThick());
0065 
0066     // prepare painter pen
0067     QPen pen(Qt::black);
0068     pen.setWidthF(style.penWidth().toPoint());
0069 
0070     QPen innerPen(Qt::white);
0071     innerPen.setWidthF(style.innerLineWidth().toPoint());
0072 
0073     for (int i = 0; i < static_cast<int>(tikz::Arrow::ArrowCount); ++i) {
0074         if (arrowHead) {
0075             style.setArrowHead(static_cast<tikz::Arrow>(i));
0076         } else {
0077             style.setArrowTail(static_cast<tikz::Arrow>(i));
0078         }
0079 
0080         // create pixmap of 1cm width with default icon height
0081         QPixmap pixmap(iconSize);
0082         pixmap.fill(Qt::transparent);
0083 
0084         // create arrow
0085         AbstractArrow* arrow = createArrow(static_cast<tikz::Arrow>(i), &style);
0086 
0087         // now paint to pixmap in Point unit
0088         QPainter p(&pixmap);
0089         p.setRenderHint(QPainter::Antialiasing, true);
0090         const qreal s = tikz::Value(1, tikz::Unit::Inch).toPoint();
0091         p.scale(q->physicalDpiX() / s, -q->physicalDpiY() / s);
0092         p.translate(0.0, -tikz::mm2pt(1) * iconHeight / h / 2.0);
0093 
0094         // draw line
0095         p.setPen(pen);
0096         if (arrowHead) {
0097             p.drawLine(QPointF(0.0, 0.0), QPointF(tikz::cm2pt(0.5) - arrow->rightExtend(), 0.0));
0098         } else {
0099             p.drawLine(QPointF(tikz::cm2pt(0.5), 0.0), QPointF(tikz::cm2pt(0.0) + arrow->rightExtend(), 0.0));
0100         }
0101 
0102         // draw arrow
0103         if (!arrowHead) {
0104             p.translate(0.0 + arrow->rightExtend(), 0);
0105             p.rotate(180);
0106         } else {
0107             p.translate(tikz::cm2pt(0.5) - arrow->rightExtend(), 0);
0108         }
0109         arrow->draw(&p);
0110         p.end();
0111 
0112         // add arrow to combo box
0113         comboBox->addItem(pixmap, arrow->name());
0114         delete arrow;
0115     }
0116 //     comboBox->view()->setMaximumWidth(1000);
0117 //     qDebug() << comboBox->view()->sizePolicy(). horizontalPolicy();
0118 //     comboBox->view()->setFixedWidth(comboBox->sizeHint().width());//setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0119 }
0120 
0121 ArrowComboBox::ArrowComboBox(bool arrowHead, QWidget* parent)
0122     : QWidget(parent)
0123     , d(new ArrowComboBoxPrivate())
0124 {
0125     d->q = this;
0126     d->arrowHead = arrowHead;
0127 
0128     QHBoxLayout * hBox = new QHBoxLayout(this);
0129     d->comboBox = new IconComboBox(this);
0130 
0131     hBox->addWidget(d->comboBox);
0132 
0133     // reload arrows
0134     d->fillComboBox();
0135 }
0136 
0137 ArrowComboBox::~ArrowComboBox()
0138 {
0139     delete d;
0140 }
0141 
0142 // void ArrowComboBox::setStyle(tikz::core::Style * style)
0143 // {
0144 //
0145 // }
0146 
0147 // kate: indent-width 4; replace-tabs on;