Warning, file /sdk/ktechlab/src/gui/itemeditor/orientationwidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2006 David Saxton <david@bluehaze.org>             *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  ***************************************************************************/
0009 
0010 #include "orientationwidget.h"
0011 #include "cnitem.h"
0012 #include "cnitemgroup.h"
0013 #include "component.h"
0014 #include "flowpart.h"
0015 #include "iteminterface.h"
0016 #include "itemlibrary.h"
0017 #include "node.h"
0018 
0019 #include <QBitmap>
0020 #include <QDebug>
0021 #include <QGridLayout>
0022 #include <QImage>
0023 #include <QPainter>
0024 #include <QPixmap>
0025 #include <QPushButton>
0026 
0027 #include <cassert>
0028 
0029 const int _size = 44;
0030 
0031 // BEGIN class OrientationWidget
0032 OrientationWidget::OrientationWidget(QWidget *parent)
0033     : QWidget(parent)
0034 {
0035     QGridLayout *layout = new QGridLayout(this /*, 2, 4, 0, 4 */);
0036     layout->setMargin(0);
0037     layout->setSpacing(4);
0038 
0039     for (int row = 0; row < 2; ++row) {
0040         for (int col = 0; col < 4; ++col) {
0041             QPushButton *btn = new QPushButton(this);
0042             m_toolBtn[row][col] = btn;
0043             layout->addWidget(btn, row, col);
0044             btn->setFixedSize(_size + 6, _size + 6);
0045             //          btn->setFlat(true);
0046             btn->setIconSize(QSize(50, 50));
0047             btn->setCheckable(true);
0048 
0049             connect(btn, &QPushButton::clicked, this, &OrientationWidget::slotButtonClicked);
0050         }
0051     }
0052     setVisible(false);
0053 }
0054 
0055 OrientationWidget::~OrientationWidget()
0056 {
0057 }
0058 
0059 void OrientationWidget::slotUpdate(CNItemGroup *itemGroup)
0060 {
0061     if (m_pCNItem)
0062         m_pCNItem->disconnect(this);
0063 
0064     m_pCNItem = dynamic_cast<CNItem *>(itemGroup->activeItem());
0065     if (m_pCNItem)
0066         connect(m_pCNItem, &CNItem::orientationChanged, this, &OrientationWidget::updateShownOrientation);
0067 
0068     bool haveSameOrientation = itemGroup->haveSameOrientation();
0069 
0070     if (FlowPart *flowPart = dynamic_cast<FlowPart *>(static_cast<CNItem *>(m_pCNItem))) {
0071         // Do we actually need to udpate the interface?
0072         if (m_pFlowPart && (m_bHaveSameOrientation == haveSameOrientation))
0073             return;
0074 
0075         m_pComponent = nullptr;
0076         m_pFlowPart = flowPart;
0077         m_bHaveSameOrientation = haveSameOrientation;
0078 
0079         initFromFlowPart(m_pFlowPart);
0080     }
0081 
0082     else if (Component *component = dynamic_cast<Component *>(static_cast<CNItem *>(m_pCNItem))) {
0083         // Do we actually need to udpate the interface?
0084         if (m_pComponent && (m_bHaveSameOrientation == haveSameOrientation))
0085             return;
0086 
0087         m_pFlowPart = nullptr;
0088         m_pComponent = component;
0089         m_bHaveSameOrientation = haveSameOrientation;
0090 
0091         initFromComponent(m_pComponent);
0092     }
0093 
0094     else
0095         slotClear();
0096 }
0097 
0098 void OrientationWidget::initFromFlowPart(FlowPart *flowPart)
0099 {
0100     if (!flowPart)
0101         return;
0102 
0103     uint valid = flowPart->allowedOrientations();
0104 
0105     for (uint i = 0; i < 2; ++i) {
0106         for (uint j = 0; j < 4; ++j) {
0107             uint o = j + 4 * i;
0108             if (valid & (1 << o)) {
0109                 m_toolBtn[i][j]->setEnabled(true);
0110                 QPixmap pm(50, 50);
0111                 flowPart->orientationPixmap(o, pm);
0112                 m_toolBtn[i][j]->setIcon(QIcon(pm));
0113             }
0114         }
0115     }
0116 
0117     updateShownOrientation();
0118 }
0119 
0120 void OrientationWidget::initFromComponent(Component *component)
0121 {
0122     const QImage im = itemLibrary()->componentImage(component);
0123 
0124     QRect bound = component->boundingRect();
0125 
0126     // We want a nice square bounding rect
0127     const int dy = bound.width() - bound.height();
0128     if (dy > 0) {
0129         bound.setTop(bound.top() - (dy / 2));
0130         bound.setBottom(bound.bottom() + (dy / 2));
0131     } else if (dy < 0) {
0132         bound.setLeft(bound.left() + (dy / 2));
0133         bound.setRight(bound.right() - (dy / 2));
0134     }
0135 
0136     QPixmap tbPm;
0137 
0138     for (unsigned col = 0; col < 4; ++col) {
0139         for (unsigned row = 0; row < 2; ++row) {
0140             bool flipped = bool(row);
0141             int angle = 90 * col;
0142 
0143             if (col || row) {
0144                 tbPm.convertFromImage(im.transformed(Component::transMatrix(angle, flipped, bound.width() / 2, bound.height() / 2)));
0145             } else {
0146                 tbPm.convertFromImage(im);
0147             }
0148 
0149             m_toolBtn[row][col]->setIcon(QIcon(tbPm));
0150         }
0151     }
0152 
0153     updateShownOrientation();
0154     setVisible(true);
0155 }
0156 
0157 void OrientationWidget::slotClear()
0158 {
0159     if (m_pCNItem)
0160         m_pCNItem->disconnect(this);
0161 
0162     m_pComponent = nullptr;
0163     m_pFlowPart = nullptr;
0164     m_pCNItem = nullptr;
0165 
0166     setVisible(false);
0167 }
0168 
0169 void OrientationWidget::slotButtonClicked()
0170 {
0171     QPushButton *btn = const_cast<QPushButton *>(dynamic_cast<const QPushButton *>(sender()));
0172     assert(btn);
0173 
0174     for (int row = 0; row < 2; ++row) {
0175         for (int col = 0; col < 4; ++col) {
0176             bool isButton = (m_toolBtn[row][col] == btn);
0177             m_toolBtn[row][col]->setChecked(isButton);
0178             if (!isButton)
0179                 continue;
0180 
0181             if (m_pFlowPart)
0182                 ItemInterface::self()->setFlowPartOrientation((4 * row) + col);
0183             else
0184                 ItemInterface::self()->setComponentOrientation(90 * col, bool(row));
0185         }
0186     }
0187 }
0188 
0189 void OrientationWidget::updateShownOrientation()
0190 {
0191     if (!m_pFlowPart && !m_pComponent)
0192         return;
0193 
0194     CNItemGroup *ig = dynamic_cast<CNItemGroup *>(ItemInterface::self()->itemGroup());
0195     if (!ig)
0196         return;
0197 
0198     bool haveSameOrientation = ig->haveSameOrientation();
0199 
0200     for (unsigned col = 0; col < 4; ++col) {
0201         for (unsigned row = 0; row < 2; ++row) {
0202             if (m_pFlowPart) {
0203                 bool setOn = haveSameOrientation && (m_pFlowPart->orientation() == (4 * row + col));
0204                 m_toolBtn[row][col]->setChecked(setOn);
0205             } else {
0206                 bool flipped = bool(row);
0207                 int angle = 90 * col;
0208 
0209                 bool setOn = haveSameOrientation && (m_pComponent->angleDegrees() == angle) && (m_pComponent->flipped() == flipped);
0210                 m_toolBtn[row][col]->setChecked(setOn);
0211             }
0212         }
0213     }
0214 }
0215 // END class OrientationWidget
0216 
0217 #include "moc_orientationwidget.cpp"