Warning, file /utilities/krusader/app/DiskUsage/radialMap/widget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2003-2004 Max Howell <max.howell@methylblue.com>
0003     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "widget.h"
0009 
0010 // QtCore
0011 #include <QEvent>
0012 #include <QTimer> //member
0013 #include <QUrl>
0014 // QtGui
0015 #include <QBitmap> //ctor - finding cursor size
0016 #include <QCursor> //slotPostMouseEvent()
0017 #include <QMouseEvent>
0018 #include <QPalette>
0019 // QtWidgets
0020 #include <QApplication> //sendEvent
0021 
0022 #include "Config.h"
0023 #include "fileTree.h"
0024 #include "radialMap.h" //constants
0025 
0026 RadialMap::Widget::Widget(QWidget *parent)
0027     : QWidget(parent)
0028     , m_tree(nullptr)
0029     , m_focus(nullptr)
0030     , m_tip(QFontMetrics(font()).height()) // needs to know cursor height
0031     , m_rootSegment(nullptr) // TODO we don't delete it, *shrug*
0032 {
0033     QPalette pal = palette();
0034     pal.setColor(backgroundRole(), Qt::white);
0035     setPalette(pal);
0036 
0037     connect(this, &Widget::created, this, &Widget::sendFakeMouseEvent);
0038     connect(this, &Widget::created, this, QOverload<>::of(&Widget::update));
0039     connect(&m_timer, &QTimer::timeout, this, &Widget::resizeTimeout);
0040 }
0041 
0042 QString RadialMap::Widget::path() const
0043 {
0044     if (m_tree == nullptr)
0045         return QString();
0046     return m_tree->fullPath();
0047 }
0048 
0049 QUrl RadialMap::Widget::url(File const *const file) const
0050 {
0051     if (file == nullptr && m_tree == nullptr)
0052         return QUrl();
0053 
0054     return QUrl::fromLocalFile(file ? file->fullPath() : m_tree->fullPath());
0055 }
0056 
0057 void RadialMap::Widget::invalidate(const bool b)
0058 {
0059     if (isValid()) {
0060         //**** have to check that only way to invalidate is this function frankly
0061         //**** otherwise you may get bugs..
0062 
0063         // disable mouse tracking
0064         setMouseTracking(false);
0065 
0066         QUrl urlInv = url();
0067 
0068         // ensure this class won't think we have a map still
0069         m_tree = nullptr;
0070         m_focus = nullptr;
0071 
0072         delete m_rootSegment;
0073         m_rootSegment = nullptr;
0074 
0075         // FIXME move this disablement thing no?
0076         //       it is confusing in other areas, like the whole createFromCache() thing
0077         m_map.invalidate(b); // b signifies whether the pixmap is made to look disabled or not
0078         if (b)
0079             update();
0080 
0081         // tell rest of Filelight
0082         emit invalidated(urlInv);
0083     }
0084 }
0085 
0086 void RadialMap::Widget::create(const Directory *tree)
0087 {
0088     // it is not the responsibility of create() to invalidate first
0089     // skip invalidation at your own risk
0090 
0091     // FIXME make it the responsibility of create to invalidate first
0092 
0093     if (tree) {
0094         m_focus = nullptr;
0095         // generate the filemap image
0096         m_map.make(tree);
0097 
0098         // this is the inner circle in the center
0099         m_rootSegment = new Segment(tree, 0, 16 * 360);
0100 
0101         setMouseTracking(true);
0102     }
0103 
0104     m_tree = tree;
0105 
0106     // tell rest of Filelight
0107     emit created(tree);
0108 }
0109 
0110 void RadialMap::Widget::createFromCache(const Directory *tree)
0111 {
0112     // no scan was necessary, use cached tree, however we MUST still emit invalidate
0113     invalidate(false);
0114     create(tree);
0115 }
0116 
0117 void RadialMap::Widget::sendFakeMouseEvent() // slot
0118 {
0119     QMouseEvent me(QEvent::MouseMove, mapFromGlobal(QCursor::pos()), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
0120     QApplication::sendEvent(this, &me);
0121 }
0122 
0123 void RadialMap::Widget::resizeTimeout() // slot
0124 {
0125     // the segments are about to erased!
0126     // this was a horrid bug, and proves the OO programming should be obeyed always!
0127     m_focus = nullptr;
0128     if (m_tree)
0129         m_map.make(m_tree, true);
0130     update();
0131 }
0132 
0133 void RadialMap::Widget::refresh(int filth)
0134 {
0135     // TODO consider a more direct connection
0136 
0137     if (!m_map.isNull()) {
0138         switch (filth) {
0139         case 1:
0140             m_focus = nullptr;
0141             if (m_tree)
0142                 m_map.make(m_tree, true); // true means refresh only
0143             break;
0144 
0145         case 2:
0146             m_map.aaPaint();
0147             break;
0148 
0149         case 3:
0150             m_map.colorise(); // FALL THROUGH!
0151         case 4:
0152             m_map.paint();
0153 
0154         default:
0155             break;
0156         }
0157 
0158         update();
0159     }
0160 }
0161 
0162 void RadialMap::Widget::zoomIn() // slot
0163 {
0164     if (m_map.m_visibleDepth > MIN_RING_DEPTH) {
0165         m_focus = nullptr;
0166         --m_map.m_visibleDepth;
0167         if (m_tree)
0168             m_map.make(m_tree);
0169         Config::defaultRingDepth = m_map.m_visibleDepth;
0170         update();
0171     }
0172 }
0173 
0174 void RadialMap::Widget::zoomOut() // slot
0175 {
0176     m_focus = nullptr;
0177     ++m_map.m_visibleDepth;
0178     if (m_tree)
0179         m_map.make(m_tree);
0180     if (m_map.m_visibleDepth > Config::defaultRingDepth)
0181         Config::defaultRingDepth = m_map.m_visibleDepth;
0182     update();
0183 }
0184 
0185 RadialMap::Segment::~Segment()
0186 {
0187     if (isFake())
0188         delete m_file; // created by us in Builder::build()
0189 }