File indexing completed on 2024-12-08 05:08:33

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
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 "canvasitemparts.h"
0012 #include "cnitem.h"
0013 #include "eventinfo.h"
0014 
0015 #include <QMouseEvent>
0016 #include <QWheelEvent>
0017 
0018 // #include <q3button.h>
0019 
0020 #include <ktechlab_debug.h>
0021 
0022 CIWidgetMgr::CIWidgetMgr(KtlQCanvas *canvas, CNItem *item)
0023 {
0024     p_cnItem = item;
0025     p_canvas = canvas;
0026 }
0027 
0028 CIWidgetMgr::~CIWidgetMgr()
0029 {
0030     // KtlQCanvas deletes our items for us. Actually, it pretty much insists on deleting them,
0031     // despite me telling it not to, so if I delete them then it gets confused and crashes.
0032     // Naughty KtlQCanvas!
0033     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0034     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0035         delete it.value();
0036     }
0037     m_widgetMap.clear();
0038 }
0039 
0040 void CIWidgetMgr::setWidgetsPos(const QPoint &pos)
0041 {
0042     m_pos = pos;
0043 }
0044 
0045 void CIWidgetMgr::setDrawWidgets(bool draw)
0046 {
0047     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0048     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0049         draw ? it.value()->show() : it.value()->hide();
0050     }
0051 }
0052 
0053 Widget *CIWidgetMgr::widgetWithID(const QString &id) const
0054 {
0055     WidgetMap::const_iterator it = m_widgetMap.find(id);
0056     if (it == m_widgetMap.end())
0057         return nullptr;
0058     else
0059         return it.value();
0060 }
0061 
0062 Button *CIWidgetMgr::button(const QString &id) const
0063 {
0064     return dynamic_cast<Button *>(widgetWithID(id));
0065 }
0066 
0067 Slider *CIWidgetMgr::slider(const QString &id) const
0068 {
0069     return dynamic_cast<Slider *>(widgetWithID(id));
0070 }
0071 
0072 void CIWidgetMgr::setButtonState(const QString &id, int state)
0073 {
0074     Button *b = button(id);
0075     if (!b)
0076         return;
0077 
0078     // Actually, we don't want to check to see if we are already down; this way,
0079     // we get toggle events when loading from file
0080     //  bool oldState = b->isDown();
0081     //  if ( oldState == state )
0082     //      return;
0083 
0084     b->setState(state);
0085 }
0086 
0087 void CIWidgetMgr::drawWidgets(QPainter &p)
0088 {
0089     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0090     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0091         it.value()->drawShape(p);
0092     }
0093 }
0094 
0095 void CIWidgetMgr::removeWidget(const QString &id)
0096 {
0097     if (!m_widgetMap.contains(id))
0098         return;
0099 
0100     delete m_widgetMap[id];
0101     m_widgetMap.remove(id);
0102 }
0103 
0104 Button *CIWidgetMgr::addButton(const QString &id, const QRect &pos, const QString &display, bool toggle)
0105 {
0106     WidgetMap::iterator it;
0107 
0108     Button *button = new Button(id, p_cnItem, toggle, pos, p_canvas);
0109     (qobject_cast<QToolButton *>(button->widget()))->setText(display);
0110 
0111     it = m_widgetMap.find(id);
0112     if (it == m_widgetMap.end()) {
0113         m_widgetMap[id] = button;
0114     } else {
0115         qCWarning(KTL_LOG) << "CIWidgetMgr::addButton: Attempting to re-add button with same id as previous";
0116         delete it.value();
0117         it.value() = button;
0118     }
0119 
0120     p_cnItem->updateAttachedPositioning();
0121     return button;
0122 }
0123 
0124 Button *CIWidgetMgr::addButton(const QString &id, const QRect &pos, const QIcon &icon, bool toggle)
0125 {
0126     WidgetMap::iterator it;
0127 
0128     Button *button = new Button(id, p_cnItem, toggle, pos, p_canvas);
0129     button->setIcon(icon);
0130 
0131     it = m_widgetMap.find(id);
0132     if (it == m_widgetMap.end()) {
0133         m_widgetMap[id] = button;
0134     } else {
0135         qCWarning(KTL_LOG) << "CIWidgetMgr::addButton: Attempting to re-add button with same id as previous";
0136         delete it.value();
0137         it.value() = button;
0138     }
0139 
0140     p_cnItem->updateAttachedPositioning();
0141     return button;
0142 }
0143 
0144 Slider *CIWidgetMgr::addSlider(const QString &id, int minValue, int maxValue, int pageStep, int value, Qt::Orientation orientation, const QRect &pos)
0145 {
0146     Slider *slider = new Slider(id, p_cnItem, pos, p_canvas);
0147     QSlider *qslider = qobject_cast<QSlider *>(slider->widget());
0148 
0149     qslider->setMinimum(minValue);
0150     qslider->setMaximum(maxValue);
0151     qslider->setPageStep(pageStep);
0152     qslider->setValue(value);
0153     slider->setOrientation(orientation);
0154 
0155     WidgetMap::iterator it = m_widgetMap.find(id);
0156     if (it == m_widgetMap.end()) {
0157         m_widgetMap[id] = slider;
0158     } else {
0159         qCWarning(KTL_LOG) << "CIWidgetMgr::addSlider: Attempting to re-add slider with same id as previous";
0160         delete slider;
0161         return nullptr;
0162     }
0163 
0164     p_cnItem->updateAttachedPositioning();
0165     return slider;
0166 }
0167 
0168 bool CIWidgetMgr::mousePressEvent(const EventInfo &info)
0169 {
0170     QMouseEvent *e = info.mousePressEvent(0, 0);
0171 
0172     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0173     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0174         if (it.value()->rect().contains(info.pos)) {
0175             it.value()->mousePressEvent(e);
0176             if (e->isAccepted()) {
0177                 delete e;
0178                 return true;
0179             }
0180         }
0181     }
0182     delete e;
0183     return false;
0184 }
0185 
0186 bool CIWidgetMgr::mouseReleaseEvent(const EventInfo &info)
0187 {
0188     QMouseEvent *e = info.mouseReleaseEvent(0, 0);
0189 
0190     qCDebug(KTL_LOG) << " button=" << e->button() << " buttons=" << e->buttons();
0191 
0192     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0193     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0194         it.value()->mouseReleaseEvent(e);
0195     }
0196 
0197     bool accepted = e->isAccepted();
0198     delete e;
0199     return accepted;
0200 }
0201 
0202 bool CIWidgetMgr::mouseDoubleClickEvent(const EventInfo &info)
0203 {
0204     QMouseEvent *e = info.mouseDoubleClickEvent(0, 0);
0205 
0206     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0207     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0208         if (it.value()->rect().contains(info.pos)) {
0209             it.value()->mouseDoubleClickEvent(e);
0210             if (e->isAccepted()) {
0211                 delete e;
0212                 return true;
0213             }
0214         }
0215     }
0216     delete e;
0217     return false;
0218 }
0219 
0220 bool CIWidgetMgr::mouseMoveEvent(const EventInfo &info)
0221 {
0222     QMouseEvent *e = info.mouseMoveEvent(0, 0);
0223 
0224     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0225     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0226         it.value()->mouseMoveEvent(e);
0227         if (e->isAccepted()) {
0228             delete e;
0229             return true;
0230         }
0231     }
0232     delete e;
0233     return false;
0234 }
0235 
0236 bool CIWidgetMgr::wheelEvent(const EventInfo &info)
0237 {
0238     QWheelEvent *e = info.wheelEvent(0, 0);
0239 
0240     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0241     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0242         if (it.value()->rect().contains(info.pos)) {
0243             it.value()->wheelEvent(e);
0244             if (e->isAccepted()) {
0245                 delete e;
0246                 return true;
0247             }
0248         }
0249     }
0250 
0251     delete e;
0252     return false;
0253 }
0254 
0255 void CIWidgetMgr::enterEvent(QEvent *)
0256 {
0257     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0258     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0259         it.value()->enterEvent(nullptr);
0260     }
0261 }
0262 
0263 void CIWidgetMgr::leaveEvent(QEvent *)
0264 {
0265     const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
0266     for (WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it) {
0267         it.value()->leaveEvent(nullptr);
0268     }
0269 }