diff --git a/src/fileTree.h b/src/fileTree.h index 241fbcc..870610d 100644 --- a/src/fileTree.h +++ b/src/fileTree.h @@ -1,161 +1,168 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * Copyright 2017 Harald Sitter * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #ifndef FILETREE_H #define FILETREE_H #include //qstrdup #include //decodeName() #include #include typedef quint64 FileSize; typedef quint64 Dirsize; //**** currently unused class Folder; class File { public: friend class Folder; public: File(const char *name, FileSize size) : m_parent(nullptr), m_name(qstrdup(name)), m_size(size) {} virtual ~File() { delete [] m_name; } Folder *parent() const { return m_parent; } /** Do not use for user visible strings. Use name instead. */ const char *name8Bit() const { return m_name; } /** Decoded name. Use when you need a QString. */ QString decodedName() const { return QFile::decodeName(m_name); } /** * Human readable name (including native separators where applicable). * Only use for display. */ QString displayName() const; FileSize size() const { return m_size; } virtual bool isFolder() const { return false; } /** * Human readable path for display (including native separators where applicable. * Only use for display. */ QString displayPath(const Folder * = nullptr) const; QString humanReadableSize() const { return KFormat().formatByteSize(m_size); } /** Builds a complete QUrl by walking up to root. */ QUrl url(const Folder *root = nullptr) const; protected: File(const char *name, FileSize size, Folder *parent) : m_parent(parent), m_name(qstrdup(name)), m_size(size) {} Folder *m_parent; //0 if this is treeRoot char *m_name; // partial path name (e.g. 'boot/' or 'foo.svg') FileSize m_size; // in units of bytes; sum of all children's sizes private: File(const File&); void operator=(const File&); }; class Folder : public File { public: Folder(const char *name) : File(name, 0), m_children(0) {} //DON'T pass the full path! + ~Folder() + { + qDeleteAll(files); + } + uint children() const { return m_children; } bool isFolder() const override { return true; } ///appends a Folder void append(Folder *d, const char *name=nullptr) { if (name) { delete [] d->m_name; d->m_name = qstrdup(name); } //directories that had a fullpath copy just their names this way m_children += d->children(); //doesn't include the dir itself d->m_parent = this; append((File*)d); //will add 1 to filecount for the dir itself } ///appends a File void append(const char *name, FileSize size) { append(new File(name, size, this)); } /// removes a file void remove(const File *f) { files.removeAll(const_cast(f)); + const FileSize childSize = f->size(); + delete f; for (Folder *d = this; d; d = d->parent()) { - d->m_size -= f->size(); + d->m_size -= childSize; d->m_children--; } } QList files; private: void append(File *p) { // This is also called by append(Folder), but only once all its children // were scanned. We do not need to forward the size change to our parent // since in turn we too only are added to our parent when we are have // been scanned already. m_children++; m_size += p->size(); files.append(p); } uint m_children; private: Folder(const Folder&); //undefined void operator=(const Folder&); //undefined }; #endif diff --git a/src/radialMap/labels.cpp b/src/radialMap/labels.cpp index 755f625..f868c24 100644 --- a/src/radialMap/labels.cpp +++ b/src/radialMap/labels.cpp @@ -1,317 +1,319 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #include #include #include #include #include "Config.h" #include "fileTree.h" #include "radialMap.h" #include "sincos.h" #include "widget.h" namespace RadialMap { class Label { public: Label(const RadialMap::Segment *s, int l) : segment(s), level(l), angle(segment->start() + (segment->length() / 2)) { } bool tooClose(const int otherAngle) const { return (angle > otherAngle - LABEL_ANGLE_MARGIN && angle < otherAngle + LABEL_ANGLE_MARGIN); } const RadialMap::Segment *segment; const unsigned int level; const int angle; int targetX, targetY, middleX, startY, startX; int textX, textY, tw, th; QString qs; }; void RadialMap::Widget::paintExplodedLabels(QPainter &paint) const { //we are a friend of RadialMap::Map QVector list; unsigned int startLevel = 0; //1. Create list of labels sorted in the order they will be rendered if (m_focus && m_focus->file() != m_tree) { //separate behavior for selected vs unselected segments //don't bother with files if (m_focus && m_focus->file() && !m_focus->file()->isFolder()) { return; } //find the range of levels we will be potentially drawing labels for //startLevel is the level above whatever m_focus is in for (const Folder *p = (const Folder*)m_focus->file(); p != m_tree; ++startLevel) { p = p->parent(); } //range=2 means 2 levels to draw labels for const uint start = m_focus->start(); const uint end = m_focus->end(); //boundary angles const uint minAngle = int(m_focus->length() * LABEL_MIN_ANGLE_FACTOR); //**** Levels should be on a scale starting with 0 //**** range is a useless parameter //**** keep a topblock var which is the lowestLevel OR startLevel for indentation purposes for (unsigned int i = startLevel; i <= m_map.m_visibleDepth; ++i) { for (const Segment *segment : m_map.m_signature[i]) { if (segment->start() >= start && segment->end() <= end) { if (segment->length() > minAngle) { list.append(new Label(segment, i)); } } } } } else { - for (Segment *segment : *m_map.m_signature) { + for (Segment *segment : m_map.m_signature[0]) { if (segment->length() > 288) { list.append(new Label(segment, 0)); } } } std::sort(list.begin(), list.end(), [](Label *item1, Label *item2) { //you add 1440 to work round the fact that later you want the circle split vertically //and as it is you start at 3 o' clock. It's to do with rightPrevY, stops annoying bug int angle1 = (item1)->angle + 1440; int angle2 = (item2)->angle + 1440; // Also sort by level if (angle1 == angle2) { return (item1->level > item2->level); } if (angle1 > 5760) angle1 -= 5760; if (angle2 > 5760) angle2 -= 5760; return (angle1 < angle2); }); //2. Check to see if any adjacent labels are too close together // if so, remove it (the least significant labels, since we sort by level too). int pos = 0; while (pos < list.size() - 1) { if (list[pos]->tooClose(list[pos+1]->angle)) { delete list.takeAt(pos+1); } else { ++pos; } } //used in next two steps bool varySizes; //**** should perhaps use doubles int *sizes = new int [ m_map.m_visibleDepth + 1 ]; //**** make sizes an array of floats I think instead (or doubles) // If the minimum is larger than the default it fucks up further down if (paint.font().pointSize() < 0 || paint.font().pointSize() < Config::minFontPitch) { QFont font = paint.font(); font.setPointSize(Config::minFontPitch); paint.setFont(font); } QVector::iterator it; do { //3. Calculate font sizes { //determine current range of levels to draw for uint range = 0; for (Label *label : list) { range = qMax(range, label->level); //**** better way would just be to assign if nothing is range } range -= startLevel; //range 0 means 1 level of labels varySizes = Config::varyLabelFontSizes && (range != 0); if (varySizes) { //create an array of font sizes for various levels //will exceed normal font pitch automatically if necessary, but not minPitch //**** this needs to be checked lots //**** what if this is negative (min size gtr than default size) uint step = (paint.font().pointSize() - Config::minFontPitch) / range; if (step == 0) { step = 1; } for (uint x = range + startLevel, y = Config::minFontPitch; x >= startLevel; y += step, --x) { sizes[x] = y; } } } //4. determine label co-ordinates const int preSpacer = int(m_map.m_ringBreadth * 0.5) + m_map.m_innerRadius; const int fullStrutLength = (m_map.width() - m_map.MAP_2MARGIN) / 2 + LABEL_MAP_SPACER; //full length of a strut from map center int prevLeftY = 0; int prevRightY = height(); QFont font; for (it = list.begin(); it != list.end(); ++it) { Label *label = *it; //** bear in mind that text is drawn with QPoint param as BOTTOM left corner of text box QString string = label->segment->file()->displayName(); if (varySizes) { font.setPointSize(sizes[label->level]); } QFontMetrics fontMetrics(font); const int minTextWidth = fontMetrics.boundingRect(QStringLiteral("M...")).width() + LABEL_TEXT_HMARGIN; // Fully elided string const int fontHeight = fontMetrics.height() + LABEL_TEXT_VMARGIN; //used to ensure label texts don't overlap const int lineSpacing = fontHeight / 4; const bool rightSide = (label->angle < 1440 || label->angle > 4320); double sinra, cosra; const double ra = M_PI/2880 * label->angle; //convert to radians sincos(ra, &sinra, &cosra); const int spacer = preSpacer + m_map.m_ringBreadth * label->level; const int centerX = m_map.width() / 2 + m_offset.x(); //centre relative to canvas const int centerY = m_map.height() / 2 + m_offset.y(); int targetX = centerX + cosra * spacer; int targetY = centerY - sinra * spacer; int startX = targetX + cosra * (fullStrutLength - spacer + m_map.m_ringBreadth / 2); int startY = targetY - sinra * (fullStrutLength - spacer); if (rightSide) { //righthand side, going upwards if (startY > prevRightY /*- fmh*/) { //then it is too low, needs to be drawn higher startY = prevRightY /*- fmh*/; } } else {//lefthand side, going downwards if (startY < prevLeftY/* + fmh*/) { //then we're too high, need to be drawn lower startY = prevLeftY /*+ fmh*/; } } int middleX = targetX - (startY - targetY) / tan(ra); int textY = startY + lineSpacing; int textX; const int textWidth = fontMetrics.boundingRect(string).width() + LABEL_TEXT_HMARGIN; if (rightSide) { if (startX + minTextWidth > width() || textY < fontHeight || middleX < targetX) { //skip this strut //**** don't duplicate this code - list.erase(it); //will delete the label and set it to list.current() which _should_ be the next ptr + it = list.erase(it); //will delete the label and set it to list.current() which _should_ be the next ptr + delete label; break; } prevRightY = textY - fontHeight - lineSpacing; //must be after above's "continue" if (m_offset.x() + m_map.width() + textWidth < width()) { startX = m_offset.x() + m_map.width(); } else { startX = qMax(width() - textWidth, startX); string = fontMetrics.elidedText(string, Qt::ElideMiddle, width() - startX); } textX = startX + LABEL_TEXT_HMARGIN; } else { // left side if (startX - minTextWidth < 0 || textY > height() || middleX > targetX) { //skip this strut - list.erase(it); //will delete the label and set it to list.current() which _should_ be the next ptr + it = list.erase(it); //will delete the label and set it to list.current() which _should_ be the next ptr + delete label; break; } prevLeftY = textY + fontHeight - lineSpacing; if (m_offset.x() - textWidth > 0) { startX = m_offset.x(); textX = startX - textWidth - LABEL_TEXT_HMARGIN; } else { textX = 0; string = fontMetrics.elidedText(string, Qt::ElideMiddle, startX); startX = fontMetrics.boundingRect(string).width() + LABEL_TEXT_HMARGIN; } } label->targetX = targetX; label->targetY = targetY; label->middleX = middleX; label->startY = startY; label->startX = startX; label->textX = textX; label->textY = textY; label->qs = string; } //if an element is deleted at this stage, we need to do this whole //iteration again, thus the following loop //**** in rare case that deleted label was last label in top level // and last in labelList too, this will not work as expected (not critical) } while (it != list.end()); //5. Render labels QFont font; for (Label *label : list) { if (varySizes) { //**** how much overhead in making new QFont each time? // (implicate sharing remember) font.setPointSize(sizes[label->level]); paint.setFont(font); } paint.drawLine(label->targetX, label->targetY, label->middleX, label->startY); paint.drawLine(label->middleX, label->startY, label->startX, label->startY); paint.drawText(label->textX, label->textY, label->qs); } qDeleteAll(list); delete [] sizes; } } diff --git a/src/radialMap/map.cpp b/src/radialMap/map.cpp index d971597..fedba58 100644 --- a/src/radialMap/map.cpp +++ b/src/radialMap/map.cpp @@ -1,469 +1,477 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #include //make() #include //make() & paint() #include //ctor #include //ctor #include #include #include "filelight_debug.h" #include //make() #include #include "radialMap.h" // defines #include "Config.h" #include "fileTree.h" #define SINCOS_H_IMPLEMENTATION (1) #include "sincos.h" #include "widget.h" RadialMap::Map::Map(bool summary) - : m_signature(nullptr) - , m_visibleDepth(DEFAULT_RING_DEPTH) + : m_visibleDepth(DEFAULT_RING_DEPTH) , m_ringBreadth(MIN_RING_BREADTH) , m_innerRadius(0) , m_summary(summary) { //FIXME this is all broken. No longer is a maximum depth! const int fmh = QFontMetrics(QFont()).height(); const int fmhD4 = fmh / 4; MAP_2MARGIN = 2 * (fmh - (fmhD4 - LABEL_MAP_SPACER)); //margin is dependent on fitting in labels at top and bottom } RadialMap::Map::~Map() { - delete [] m_signature; + for (QList &segments : m_signature) { + qDeleteAll(segments); + } + m_signature.clear(); } void RadialMap::Map::invalidate() { - delete [] m_signature; - m_signature = nullptr; + for (QList &segments : m_signature) { + qDeleteAll(segments); + } + m_signature.clear(); m_visibleDepth = Config::defaultRingDepth; } void RadialMap::Map::make(const Folder *tree, bool refresh) { //slow operation so set the wait cursor QApplication::setOverrideCursor(Qt::WaitCursor); //build a signature of visible components { //**** REMOVE NEED FOR the +1 with MAX_RING_DEPTH uses //**** add some angle bounds checking (possibly in Segment ctor? can I delete in a ctor?) //**** this is a mess - delete [] m_signature; - m_signature = new QList[m_visibleDepth + 1]; + for (QList &segments : m_signature) { + qDeleteAll(segments); + } + m_signature.clear(); + + m_signature.resize(m_visibleDepth + 1); m_root = tree; if (!refresh) { m_minSize = (tree->size() * 3) / (PI * height() - MAP_2MARGIN); findVisibleDepth(tree); } setRingBreadth(); // Calculate ring size limits m_limits.resize(m_visibleDepth + 1); const double size = m_root->size(); const double pi2B = M_PI * 4 * m_ringBreadth; for (uint depth = 0; depth <= m_visibleDepth; ++depth) { m_limits[depth] = uint(size / double(pi2B * (depth + 1))); //min is angle that gives 3px outer diameter for that depth } build(tree); } //colour the segments colorise(); m_centerText = tree->humanReadableSize(); //paint the pixmap paint(); QApplication::restoreOverrideCursor(); } void RadialMap::Map::setRingBreadth() { //FIXME called too many times on creation m_ringBreadth = (height() - MAP_2MARGIN) / (2 * m_visibleDepth + 4); m_ringBreadth = qBound(MIN_RING_BREADTH, m_ringBreadth, MAX_RING_BREADTH); } void RadialMap::Map::findVisibleDepth(const Folder *dir, uint currentDepth) { //**** because I don't use the same minimumSize criteria as in the visual function // this can lead to incorrect visual representation //**** BUT, you can't set those limits until you know m_depth! //**** also this function doesn't check to see if anything is actually visible // it just assumes that when it reaches a new level everything in it is visible // automatically. This isn't right especially as there might be no files in the // dir provided to this function! static uint stopDepth = 0; if (dir == m_root) { stopDepth = m_visibleDepth; m_visibleDepth = 0; } if (m_visibleDepth < currentDepth) m_visibleDepth = currentDepth; if (m_visibleDepth >= stopDepth) return; for (File *file : dir->files) { if (file->isFolder() && file->size() > m_minSize) { findVisibleDepth((Folder *)file, currentDepth + 1); //if no files greater than min size the depth is still recorded } } } //**** segments currently overlap at edges (i.e. end of first is start of next) bool RadialMap::Map::build(const Folder * const dir, const uint depth, uint a_start, const uint a_end) { //first iteration: dir == m_root if (dir->children() == 0) //we do fileCount rather than size to avoid chance of divide by zero later return false; FileSize hiddenSize = 0; uint hiddenFileCount = 0; for (File *file : dir->files) { if (file->size() < m_limits[depth] * 6) { // limit is half a degree? we want at least 3 degrees hiddenSize += file->size(); if (file->isFolder()) { //**** considered virtual, but dir wouldn't count itself! hiddenFileCount += static_cast(file)->children(); //need to add one to count the dir as well } ++hiddenFileCount; continue; } unsigned int a_len = (unsigned int)(5760 * ((double)file->size() / (double)m_root->size())); Segment *s = new Segment(file, a_start, a_len); m_signature[depth].append(s); if (file->isFolder()) { if (depth != m_visibleDepth) { //recurse s->m_hasHiddenChildren = build((Folder*)file, depth + 1, a_start, a_start + a_len); } else { s->m_hasHiddenChildren = true; } } a_start += a_len; //**** should we add 1? } if (hiddenFileCount == dir->children() && !Config::showSmallFiles) { return true; } if ((depth == 0 || Config::showSmallFiles) && hiddenSize >= m_limits[depth] && hiddenFileCount > 0) { //append a segment for unrepresented space - a "fake" segment const QString s = i18np("1 file, with an average size of %2", "%1 files, with an average size of %2", hiddenFileCount, KFormat().formatByteSize(hiddenSize/hiddenFileCount)); - (m_signature + depth)->append(new Segment(new File(QFile::encodeName(s).constData(), hiddenSize), a_start, a_end - a_start, true)); + m_signature[depth].append(new Segment(new File(QFile::encodeName(s).constData(), hiddenSize), a_start, a_end - a_start, true)); } return false; } bool RadialMap::Map::resize(const QRectF &newRect) { //there's a MAP_2MARGIN border if (newRect.width() < width() && newRect.height() < height() && !newRect.contains(m_rect)) { return false; } uint size = qMin(newRect.width(), newRect.height()) - MAP_2MARGIN; //this also causes uneven sizes to always resize when resizing but map is small in that dimension //size -= size % 2; //even sizes mean less staggered non-antialiased resizing const uint minSize = MIN_RING_BREADTH * 2 * (m_visibleDepth + 2); if (size < minSize) { size = minSize; } //this QRectF is used by paint() m_rect.setRect(0,0,size,size); m_pixmap = QPixmap(m_rect.width() * m_dpr, m_rect.height() * m_dpr); m_pixmap.setDevicePixelRatio(m_dpr); //resize the pixmap size += MAP_2MARGIN; - if (m_signature) { + if (!m_signature.isEmpty()) { setRingBreadth(); paint(); } return true; } void RadialMap::Map::colorise() { - if (!m_signature || m_signature->isEmpty()) { + if (m_signature.isEmpty()) { qCDebug(FILELIGHT_LOG) << "no signature yet"; return; } QColor cp, cb; double darkness = 1; double contrast = (double)Config::contrast / (double)100; int h, s1, s2, v1, v2; QPalette palette; const QColor kdeColour[2] = { palette.windowText().color(), palette.window().color() }; double deltaRed = (double)(kdeColour[0].red() - kdeColour[1].red()) / 2880; //2880 for semicircle double deltaGreen = (double)(kdeColour[0].green() - kdeColour[1].green()) / 2880; double deltaBlue = (double)(kdeColour[0].blue() - kdeColour[1].blue()) / 2880; if (m_summary) { // Summary view has its own colors, special cased. cp = Qt::gray; cb = Qt::white; m_signature[0][0]->setPalette(cp, cb); // need to check in case there's no free space if (m_signature[0].size() > 1) { cb = QApplication::palette().highlight().color(); cb.getHsv(&h, &s1, &v1); if (s1 > 80) { s1 = 80; } v2 = v1 - int(contrast * v1); s2 = s1 + int(contrast * (255 - s1)); cb.setHsv(h, s1, v1); cp.setHsv(h, s2, v2); m_signature[0][1]->setPalette(cp, cb); } return; } for (uint i = 0; i <= m_visibleDepth; ++i, darkness += 0.04) { for (Segment *segment : m_signature[i]) { switch (Config::scheme) { case Filelight::KDE: { //gradient will work by figuring out rgb delta values for 360 degrees //then each component is angle*delta int a = segment->start(); if (a > 2880) a = 2880 - (a - 2880); h = (int)(deltaRed * a) + kdeColour[1].red(); s1 = (int)(deltaGreen * a) + kdeColour[1].green(); v1 = (int)(deltaBlue * a) + kdeColour[1].blue(); cb.setRgb(h, s1, v1); cb.getHsv(&h, &s1, &v1); break; } case Filelight::HighContrast: cp.setHsv(0, 0, 0); //values of h, s and v are irrelevant cb.setHsv(180, 0, int(255.0 * contrast)); segment->setPalette(cp, cb); continue; default: h = int(segment->start() / 16); s1 = 160; v1 = (int)(255.0 / darkness); //****doing this more often than once seems daft! } v2 = v1 - int(contrast * v1); s2 = s1 + int(contrast * (255 - s1)); if (s1 < 80) s1 = 80; //can fall too low and makes contrast between the files hard to discern if (segment->isFake()) { //multi-file cb.setHsv(h, s2, (v2 < 90) ? 90 : v2); //too dark if < 100 cp.setHsv(h, 17, v1); } else if (!segment->file()->isFolder()) { //file cb.setHsv(h, 17, v1); cp.setHsv(h, 17, v2); } else { //folder cb.setHsv(h, s1, v1); //v was 225 cp.setHsv(h, s2, v2); //v was 225 - delta } segment->setPalette(cp, cb); //TODO: //**** may be better to store KDE colours as H and S and vary V as others //**** perhaps make saturation difference for s2 dependent on contrast too //**** fake segments don't work with highContrast //**** may work better with cp = cb rather than Qt::white //**** you have to ensure the grey of files is sufficient, currently it works only with rainbow (perhaps use contrast there too) //**** change v1,v2 to vp, vb etc. //**** using percentages is not strictly correct as the eye doesn't work like that //**** darkness factor is not done for kde_colour scheme, and also value for files is incorrect really for files in this scheme as it is not set like rainbow one is } } } void RadialMap::Map::paint(bool antialias) { KColorScheme scheme(QPalette::Active, KColorScheme::View); QPainter paint; QRectF rect = m_rect; rect.adjust(MAP_HIDDEN_TRIANGLE_SIZE, MAP_HIDDEN_TRIANGLE_SIZE, -MAP_HIDDEN_TRIANGLE_SIZE, -MAP_HIDDEN_TRIANGLE_SIZE); m_pixmap.fill(Qt::transparent); //m_rect.moveRight(1); // Uncommenting this breaks repainting when recreating map from cache //**** best option you can think of is to make the circles slightly less perfect, // ** i.e. slightly eliptic when resizing inbetween if (m_pixmap.isNull()) return; if (!paint.begin(&m_pixmap)) { qCWarning(FILELIGHT_LOG) << "Filelight::RadialMap Failed to initialize painting, returning..."; return; } if (antialias && Config::antialias) { paint.translate(0.7, 0.7); paint.setRenderHint(QPainter::Antialiasing); } int step = m_ringBreadth; int excess = -1; //do intelligent distribution of excess to prevent nasty resizing if (m_ringBreadth != MAX_RING_BREADTH && m_ringBreadth != MIN_RING_BREADTH) { excess = int(rect.width()) % m_ringBreadth; ++step; } for (int x = m_visibleDepth; x >= 0; --x) { int width = rect.width() / 2; //clever geometric trick to find largest angle that will give biggest arrow head uint a_max = int(acos((double)width / double((width + MAP_HIDDEN_TRIANGLE_SIZE))) * (180*16 / M_PI)); for (Segment *segment : m_signature[x]) { //draw the pie segments, most of this code is concerned with drawing the little //arrows on the ends of segments when they have hidden files paint.setPen(segment->pen()); paint.setPen(segment->pen()); paint.setBrush(segment->brush()); paint.drawPie(rect, segment->start(), segment->length()); if (!segment->hasHiddenChildren()) { continue; } //draw arrow head to indicate undisplayed files/directories QPolygonF pts; QPointF pos, cpos = rect.center(); uint a[3] = { segment->start(), segment->length(), 0 }; a[2] = a[0] + (a[1] / 2); //assign to halfway between if (a[1] > a_max) { a[1] = a_max; a[0] = a[2] - a_max / 2; } a[1] += a[0]; for (int i = 0, radius = width; i < 3; ++i) { double ra = M_PI/(180*16) * a[i], sinra, cosra; if (i == 2) { radius += MAP_HIDDEN_TRIANGLE_SIZE; } sincos(ra, &sinra, &cosra); pos.rx() = cpos.x() + cosra * radius; pos.ry() = cpos.y() - sinra * radius; pts << pos; } paint.setBrush(segment->pen()); paint.drawPolygon(pts); // Draw outline on the arc for hidden children QPen pen = paint.pen(); pen.setCapStyle(Qt::FlatCap); int width = 2; pen.setWidth(width); paint.setPen(pen); QRectF rect2 = rect; width /= 2; rect2.adjust(width, width, -width, -width); paint.drawArc(rect2, segment->start(), segment->length()); } if (excess >= 0) { //excess allows us to resize more smoothly (still crud tho) if (excess < 2) //only decrease rect by more if even number of excesses left --step; excess -= 2; } rect.adjust(step, step, -step, -step); } // if(excess > 0) rect.addCoords(excess, excess, 0, 0); //ugly paint.setPen(scheme.foreground().color()); paint.setBrush(scheme.background().color()); paint.drawEllipse(rect); paint.drawText(rect, Qt::AlignCenter, m_centerText); m_innerRadius = rect.width() / 2; //rect.width should be multiple of 2 paint.end(); } diff --git a/src/radialMap/map.h b/src/radialMap/map.h index 37f540a..c44e87f 100644 --- a/src/radialMap/map.h +++ b/src/radialMap/map.h @@ -1,88 +1,88 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #ifndef MAP_H #define MAP_H #include "fileTree.h" #include #include #include #include namespace RadialMap { class Segment; class Map { public: explicit Map(bool summary); ~Map(); void make(const Folder *, bool = false); bool resize(const QRectF&); bool isNull() const { - return (m_signature == nullptr); + return (m_signature.isEmpty()); } void invalidate(); qreal height() const { return m_rect.height(); } qreal width() const { return m_rect.width(); } QPixmap pixmap() const { return m_pixmap; } friend class Widget; private: void paint(bool antialias = true); void colorise(); void setRingBreadth(); void findVisibleDepth(const Folder *dir, uint currentDepth = 0); bool build(const Folder* const dir, const uint depth =0, uint a_start =0, const uint a_end =5760); - QList *m_signature; + QVector> m_signature; const Folder *m_root; uint m_minSize; QVector m_limits; QRectF m_rect; uint m_visibleDepth; ///visible level depth of system QPixmap m_pixmap; int m_ringBreadth; uint m_innerRadius; ///radius of inner circle QString m_centerText; bool m_summary; qreal m_dpr; uint MAP_2MARGIN; }; } #endif diff --git a/src/radialMap/radialMap.h b/src/radialMap/radialMap.h index 2cc6622..28fbbb4 100644 --- a/src/radialMap/radialMap.h +++ b/src/radialMap/radialMap.h @@ -1,111 +1,111 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #ifndef RADIALMAP_H #define RADIALMAP_H #include class File; namespace RadialMap { class Segment //all angles are in 16ths of degrees { public: Segment(const File *f, uint s, uint l, bool isFake = false) : m_angleStart(s) , m_angleSegment(l) , m_file(f) , m_hasHiddenChildren(false) , m_fake(isFake) {} ~Segment(); uint start() const { return m_angleStart; } uint length() const { return m_angleSegment; } uint end() const { return m_angleStart + m_angleSegment; } const File *file() const { return m_file; } const QColor& pen() const { return m_pen; } const QColor& brush() const { return m_brush; } bool isFake() const { return m_fake; } bool hasHiddenChildren() const { return m_hasHiddenChildren; } bool intersects(uint a) const { return ((a >= start()) && (a < end())); } friend class Map; friend class Builder; private: void setPalette(const QColor &p, const QColor &b) { m_pen = p; m_brush = b; } const uint m_angleStart, m_angleSegment; - const File* const m_file; + const File* const m_file = nullptr; QColor m_pen, m_brush; bool m_hasHiddenChildren; const bool m_fake; }; } #ifndef PI #define PI 3.141592653589793 #endif #ifndef M_PI #define M_PI 3.14159265358979323846264338327 #endif #define MIN_RING_BREADTH 20 #define MAX_RING_BREADTH 60 #define DEFAULT_RING_DEPTH 4 //first level = 0 #define MIN_RING_DEPTH 0 #define MAP_HIDDEN_TRIANGLE_SIZE 5 #define LABEL_MAP_SPACER 7 #define LABEL_TEXT_HMARGIN 5 #define LABEL_TEXT_VMARGIN 0 #define LABEL_ANGLE_MARGIN 32 #define LABEL_MIN_ANGLE_FACTOR 0.05 #define LABEL_MAX_CHARS 30 #endif diff --git a/src/radialMap/widgetEvents.cpp b/src/radialMap/widgetEvents.cpp index e2c7a6c..ba425ca 100644 --- a/src/radialMap/widgetEvents.cpp +++ b/src/radialMap/widgetEvents.cpp @@ -1,410 +1,409 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #include "fileTree.h" #include "Config.h" #include "radialMap.h" //class Segment #include "widget.h" #include //::mouseMoveEvent() #include #include //::mousePressEvent() #include #include #include //::mousePressEvent() #include //::mousePressEvent() #include //::mousePressEvent() #include #include #include #include #include //QApplication::setOverrideCursor() #include #include #include //::resizeEvent() #include #include #include #include #include #include #include #include #include #include //::segmentAt() void RadialMap::Widget::resizeEvent(QResizeEvent*) { if (m_map.resize(rect())) m_timer.setSingleShot(true); m_timer.start(500); //will cause signature to rebuild for new size //always do these as they need to be initialised on creation m_offset.rx() = (width() - m_map.width()) / 2; m_offset.ry() = (height() - m_map.height()) / 2; } void RadialMap::Widget::paintEvent(QPaintEvent*) { QPainter paint; paint.begin(this); if (!m_map.isNull()) paint.drawPixmap(m_offset, m_map.pixmap()); else { paint.drawText(rect(), 0, i18nc("We messed up, the user needs to initiate a rescan.", "Internal representation is invalid,\nplease rescan.")); return; } //exploded labels if (!m_map.isNull() && !m_timer.isActive()) { if (Config::antialias) { paint.setRenderHint(QPainter::Antialiasing); //make lines appear on pixel boundaries paint.translate(0.5, 0.5); } paintExplodedLabels(paint); } } const RadialMap::Segment* RadialMap::Widget::segmentAt(QPointF e) const { //determine which segment QPointF e is above e -= m_offset; - if (!m_map.m_signature) + if (m_map.m_signature.isEmpty()) return nullptr; if (e.x() <= m_map.width() && e.y() <= m_map.height()) { //transform to cartesian coords e.rx() -= m_map.width() / 2; //should be an int e.ry() = m_map.height() / 2 - e.y(); double length = hypot(e.x(), e.y()); if (length >= m_map.m_innerRadius) //not hovering over inner circle { uint depth = ((int)length - m_map.m_innerRadius) / m_map.m_ringBreadth; if (depth <= m_map.m_visibleDepth) //**** do earlier since you can //** check not outside of range { //vector calculation, reduces to simple trigonometry //cos angle = (aibi + ajbj) / albl //ai = x, bi=1, aj=y, bj=0 //cos angle = x / (length) uint a = (uint)(acos((double)e.x() / length) * 916.736); //916.7324722 = #radians in circle * 16 //acos only understands 0-180 degrees if (e.y() < 0) a = 5760 - a; for (Segment *segment : m_map.m_signature[depth]) { if (segment->intersects(a)) return segment; } } } else return m_rootSegment; //hovering over inner circle } return nullptr; } void RadialMap::Widget::mouseMoveEvent(QMouseEvent *e) { //set m_focus to what we hover over, update UI if it's a new segment Segment const * const oldFocus = m_focus; m_focus = segmentAt(e->pos()); if (!m_focus) { if (oldFocus && oldFocus->file() != m_tree) { m_tooltip.hide(); unsetCursor(); update(); emit mouseHover(QString()); } return; } const QRectF screenRect = window()->windowHandle()->screen()->availableGeometry(); QPoint tooltipPosition = e->globalPos() + QPoint(20, 20); QRectF tooltipRect(tooltipPosition, m_tooltip.size()); // Same content as before if (m_focus == oldFocus) { if (tooltipRect.right() > screenRect.right()) { tooltipPosition.setX(screenRect.width() - m_tooltip.width()); } if (tooltipRect.bottom() > screenRect.bottom()) { tooltipPosition.setY(screenRect.height() - m_tooltip.height()); } m_tooltip.move(tooltipPosition); return; } setCursor(Qt::PointingHandCursor); QString string; if (isSummary()) { if (strcmp("used", m_focus->file()->name8Bit()) == 0) { string = i18nc("Tooltip of used space on the partition, %1 is path, %2 is size", "%1\nUsed: %2", m_focus->file()->parent()->displayPath(), m_focus->file()->humanReadableSize()); } else if (strcmp("free", m_focus->file()->name8Bit()) == 0) { string = i18nc("Tooltip of free space on the partition, %1 is path, %2 is size", "%1\nFree: %2", m_focus->file()->parent()->displayPath(), m_focus->file()->humanReadableSize()); } else { string = i18nc("Tooltip of file/folder, %1 is path, %2 is size", "%1\n%2", m_focus->file()->displayPath(), m_focus->file()->humanReadableSize()); } } else { string = i18nc("Tooltip of file/folder, %1 is path, %2 is size", "%1\n%2", m_focus->file()->displayPath(), m_focus->file()->humanReadableSize()); if (m_focus->file()->isFolder()) { int files = static_cast(m_focus->file())->children(); const uint percent = uint((100 * files) / (double)m_tree->children()); string += QLatin1Char('\n'); if (percent > 0) { string += i18ncp("Tooltip of folder, %1 is number of files", "%1 File (%2%)", "%1 Files (%2%)", files, percent); } else { string += i18ncp("Tooltip of folder, %1 is number of files", "%1 File", "%1 Files", files); } } const QUrl url = Widget::url(m_focus->file()); if (m_focus == m_rootSegment && url != KIO::upUrl(url)) { string += i18n("\nClick to go up to parent directory"); } } // Calculate a semi-sane size for the tooltip QFontMetrics fontMetrics(font()); int tooltipWidth = 0; int tooltipHeight = 0; for (const QString &part : string.split(QLatin1Char('\n'))) { tooltipHeight += fontMetrics.height(); tooltipWidth = qMax(tooltipWidth, fontMetrics.boundingRect(part).width()); } tooltipWidth += 10; tooltipHeight += 10; m_tooltip.resize(tooltipWidth, tooltipHeight); m_tooltip.setText(string); // Make sure we're visible on screen tooltipRect.setSize(QSize(tooltipWidth, tooltipHeight)); if (tooltipRect.right() > screenRect.right()) { tooltipPosition.setX(screenRect.width() - m_tooltip.width()); } if (tooltipRect.bottom() > screenRect.bottom()) { tooltipPosition.setY(screenRect.height() - m_tooltip.height()); } m_tooltip.move(tooltipPosition); m_tooltip.show(); emit mouseHover(m_focus->file()->displayPath()); update(); } void RadialMap::Widget::enterEvent(QEvent *) { if (!m_focus) return; setCursor(Qt::PointingHandCursor); emit mouseHover(m_focus->file()->displayPath()); update(); } void RadialMap::Widget::leaveEvent(QEvent *) { m_tooltip.hide(); } void RadialMap::Widget::mousePressEvent(QMouseEvent *e) { if (!isEnabled()) return; //m_focus is set correctly (I've been strict, I assure you it is correct!) if (!m_focus || m_focus->isFake()) { return; } const QUrl url = Widget::url(m_focus->file()); const bool isDir = m_focus->file()->isFolder(); // Open file if (e->button() == Qt::MidButton || (e->button() == Qt::LeftButton && !isDir)) { new KRun(url, this, true); return; } if (e->button() == Qt::LeftButton) { if (m_focus->file() != m_tree) { emit activated(url); //activate first, this will cause UI to prepare itself createFromCache((Folder *)m_focus->file()); } else if (KIO::upUrl(url) != url) { emit giveMeTreeFor(KIO::upUrl(url)); } return; } if (e->button() != Qt::RightButton) { // Ignore other mouse buttons return; } // Actions in the right click menu QAction* openFileManager = nullptr; QAction* openTerminal = nullptr; QAction* centerMap = nullptr; QAction* openFile = nullptr; QAction* copyClipboard = nullptr; QAction* deleteItem = nullptr; QMenu popup; popup.setTitle(m_focus->file()->displayPath(m_tree)); if (isDir) { openFileManager = popup.addAction(QIcon::fromTheme(QStringLiteral("system-file-manager")), i18n("Open &File Manager Here")); if (url.scheme() == QLatin1String("file")) { openTerminal = popup.addAction(QIcon::fromTheme(QStringLiteral( "utilities-terminal" )), i18n("Open &Terminal Here")); } if (m_focus->file() != m_tree) { popup.addSeparator(); centerMap = popup.addAction(QIcon::fromTheme(QStringLiteral( "zoom-in" )), i18n("&Center Map Here")); } } else { openFile = popup.addAction(QIcon::fromTheme(QStringLiteral("document-open")), i18nc("Scan/open the path of the selected element", "&Open")); } popup.addSeparator(); copyClipboard = popup.addAction(QIcon::fromTheme(QStringLiteral( "edit-copy" )), i18n("&Copy to clipboard")); if (m_focus->file() != m_tree) { popup.addSeparator(); deleteItem = popup.addAction(QIcon::fromTheme(QStringLiteral( "edit-delete" )), i18n("&Delete")); } QAction* clicked = popup.exec(e->globalPos(), nullptr); if (openFileManager && clicked == openFileManager) { KRun::runUrl(url, QStringLiteral("inode/directory"), this #if KIO_VERSION >= QT_VERSION_CHECK(5, 31, 0) , KRun::RunFlags() #endif ); } else if (openTerminal && clicked == openTerminal) { KToolInvocation::invokeTerminal(QString(),url.path()); } else if (centerMap && clicked == centerMap) { emit activated(url); //activate first, this will cause UI to prepare itself createFromCache((Folder *)m_focus->file()); } else if (openFile && clicked == openFile) { new KRun(url, this, true); } else if (clicked == copyClipboard) { QMimeData* mimedata = new QMimeData(); mimedata->setUrls(QList() << url); QApplication::clipboard()->setMimeData(mimedata , QClipboard::Clipboard); } else if (clicked == deleteItem && m_focus->file() != m_tree) { m_toBeDeleted = m_focus; const QUrl url = Widget::url(m_toBeDeleted->file()); const QString message = m_toBeDeleted->file()->isFolder() ? i18n("The folder at '%1' will be recursively and permanently deleted.", url.toString()) : i18n("'%1' will be permanently deleted.", url.toString()); const int userIntention = KMessageBox::warningContinueCancel( this, message, QString(), KGuiItem(i18n("&Delete"), QStringLiteral("edit-delete"))); if (userIntention == KMessageBox::Continue) { KIO::Job *job = KIO::del(url); connect(job, &KJob::finished, this, &RadialMap::Widget::deleteJobFinished); QApplication::setOverrideCursor(Qt::BusyCursor); setEnabled(false); } } else { //ensure m_focus is set for new mouse position sendFakeMouseEvent(); } } void RadialMap::Widget::deleteJobFinished(KJob *job) { QApplication::restoreOverrideCursor(); setEnabled(true); if (!job->error() && m_toBeDeleted) { m_toBeDeleted->file()->parent()->remove(m_toBeDeleted->file()); - delete m_toBeDeleted->file(); m_toBeDeleted = nullptr; m_focus = nullptr; m_map.make(m_tree, true); update(); } else KMessageBox::error(this, job->errorString(), i18n("Error while deleting")); } void RadialMap::Widget::dropEvent(QDropEvent *e) { QList uriList = KUrlMimeData::urlsFromMimeData(e->mimeData()); if (!uriList.isEmpty()) emit giveMeTreeFor(uriList.first()); } void RadialMap::Widget::dragEnterEvent(QDragEnterEvent *e) { QList uriList = KUrlMimeData::urlsFromMimeData(e->mimeData()); e->setAccepted(!uriList.isEmpty()); } void RadialMap::Widget::changeEvent(QEvent *e) { if (e->type() == QEvent::ApplicationPaletteChange || e->type() == QEvent::PaletteChange) m_map.paint(); } diff --git a/src/scan.cpp b/src/scan.cpp index 1362ff9..5c128a4 100644 --- a/src/scan.cpp +++ b/src/scan.cpp @@ -1,228 +1,229 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #include "scan.h" #include "remoteLister.h" #include "fileTree.h" #include "localLister.h" #include "filelight_debug.h" #include #include #include #include namespace Filelight { ScanManager::ScanManager(QObject *parent) : QObject(parent) , m_abort(false) , m_files(0) , m_mutex() , m_thread(nullptr) { Filelight::LocalLister::readMounts(); connect(this, &ScanManager::branchCacheHit, this, &ScanManager::foundCached, Qt::QueuedConnection); } ScanManager::~ScanManager() { if (m_thread) { qCDebug(FILELIGHT_LOG) << "Attempting to abort scan operation..."; m_abort = true; m_thread->wait(); } + qDeleteAll(m_cache); //RemoteListers are QObjects and get automatically deleted } bool ScanManager::running() const { return m_thread && m_thread->isRunning(); } bool ScanManager::start(const QUrl &url) { QMutexLocker locker(&m_mutex); // The m_mutex gets released once locker is destroyed (goes out of scope). //url is guaranteed clean and safe qCDebug(FILELIGHT_LOG) << "Scan requested for: " << url; if (running()) { qCWarning(FILELIGHT_LOG) << "Tried to launch two concurrent scans, aborting old one..."; abort(); } m_files = 0; m_abort = false; if (!url.isLocalFile()) { QGuiApplication::changeOverrideCursor(Qt::BusyCursor); //will start listing straight away Filelight::RemoteLister *remoteLister = new Filelight::RemoteLister(url, (QWidget*)parent(), this); connect(remoteLister, &Filelight::RemoteLister::branchCompleted, this, &ScanManager::cacheTree, Qt::QueuedConnection); remoteLister->setParent(this); remoteLister->setObjectName(QStringLiteral( "remote_lister" )); remoteLister->openUrl(url); return true; } QString path = url.toLocalFile(); if (!path.endsWith(QDir::separator())) path += QDir::separator(); QList *trees = new QList; /* CHECK CACHE * user wants: /usr/local/ * cached: /usr/ * * user wants: /usr/ * cached: /usr/local/, /usr/include/ */ QMutableListIterator it(m_cache); while (it.hasNext()) { Folder *folder = it.next(); QString cachePath = folder->decodedName(); if (path.startsWith(cachePath)) { //then whole tree already scanned //find a pointer to the requested branch qCDebug(FILELIGHT_LOG) << "Cache-(a)hit: " << cachePath; QVector split = path.midRef(cachePath.length()).split(QLatin1Char('/')); Folder *d = folder; while (!split.isEmpty() && d != nullptr) { //if NULL we have got lost so abort!! if (split.first().isEmpty()) { //found the dir break; } QString s = split.first() % QLatin1Char('/'); // % is the string concatenation operator for QStringBuilder QListIterator it(d->files); d = nullptr; while (it.hasNext()) { File *subfolder = it.next(); if (s == subfolder->decodedName()) { d = (Folder*)subfolder; break; } } split.pop_front(); } if (d) { delete trees; //we found a completed tree, thus no need to scan qCDebug(FILELIGHT_LOG) << "Found cache-handle, generating map.."; emit branchCacheHit(d); return true; } else { //something went wrong, we couldn't find the folder we were expecting qCWarning(FILELIGHT_LOG) << "Didn't find " << path << " in the cache!\n"; it.remove(); emit aboutToEmptyCache(); delete folder; break; //do a full scan } } else if (cachePath.startsWith(path)) { //then part of the requested tree is already scanned qCDebug(FILELIGHT_LOG) << "Cache-(b)hit: " << cachePath; it.remove(); trees->append(folder); } } QGuiApplication::changeOverrideCursor(QCursor(Qt::BusyCursor)); //starts listing by itself m_thread = new Filelight::LocalLister(path, trees, this); connect(m_thread, &LocalLister::branchCompleted, this, &ScanManager::cacheTree, Qt::QueuedConnection); m_thread->start(); return true; } bool ScanManager::abort() { m_abort = true; delete findChild(QStringLiteral( "remote_lister" )); return m_thread && m_thread->wait(); } void ScanManager::emptyCache() { m_abort = true; if (m_thread && m_thread->isRunning()) { m_thread->wait(); } emit aboutToEmptyCache(); qDeleteAll(m_cache); m_cache.clear(); } void ScanManager::cacheTree(Folder *tree) { QMutexLocker locker(&m_mutex); // This gets released once it is destroyed. if (m_thread) { qCDebug(FILELIGHT_LOG) << "Waiting for thread to terminate ..."; m_thread->wait(); qCDebug(FILELIGHT_LOG) << "Thread terminated!"; delete m_thread; //note the lister deletes itself m_thread = nullptr; } emit completed(tree); if (tree) { //we don't cache foreign stuff //we don't recache stuff (thus only type 1000 events) m_cache.append(tree); } else { //scan failed qDeleteAll(m_cache); m_cache.clear(); } QGuiApplication::restoreOverrideCursor(); } void ScanManager::foundCached(Folder *tree) { emit completed(tree); QGuiApplication::restoreOverrideCursor(); } } diff --git a/src/summaryWidget.cpp b/src/summaryWidget.cpp index 852d9ff..3ad8934 100644 --- a/src/summaryWidget.cpp +++ b/src/summaryWidget.cpp @@ -1,175 +1,181 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #include "summaryWidget.h" #include "Config.h" #include "fileTree.h" #include "radialMap/radialMap.h" #include "radialMap/widget.h" #include #include #include #include #include #include #include #include #include namespace Filelight { struct Disk { QString mount; QString name; qint64 size; qint64 used; qint64 free; //NOTE used+avail != size (clustersize!) }; struct DiskList : QMap { DiskList(); }; class MyRadialMap : public RadialMap::Widget { Q_OBJECT public: MyRadialMap(QWidget *parent) : RadialMap::Widget(parent, true) { } virtual void setCursor(const QCursor &c) { if (focusSegment() && focusSegment()->file()->decodedName() == QLatin1String( "Used" )) RadialMap::Widget::setCursor(c); else unsetCursor(); } void mousePressEvent(QMouseEvent *e) override { if (focusSegment() == rootSegment() && e->button() == Qt::RightButton) { // we will allow right clicks to the center circle RadialMap::Widget::mousePressEvent(e); } else if (e->button() == Qt::LeftButton) { // and clicks to the used segment emit activated(url()); } } }; SummaryWidget::SummaryWidget(QWidget *parent) : QWidget(parent) { qApp->setOverrideCursor(Qt::WaitCursor); setLayout(new QGridLayout(this)); createDiskMaps(); qApp->restoreOverrideCursor(); } +SummaryWidget::~SummaryWidget() +{ + qDeleteAll(m_disksFolders); +} + void SummaryWidget::createDiskMaps() { DiskList disks; QString text; for (DiskList::ConstIterator it = disks.constBegin(), end = disks.constEnd(); it != end; ++it) { Disk const &disk = it.value(); if (disk.free == 0 && disk.used == 0) continue; QWidget *volume = new QWidget(this); QVBoxLayout *volumeLayout = new QVBoxLayout(volume); RadialMap::Widget *map = new MyRadialMap(this); QWidget *info = new QWidget(this); info->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); QHBoxLayout* horizontalLayout = new QHBoxLayout(info); // Create the text under the radialMap. if (disk.name.isEmpty()) { text = i18nc("Percent used disk space on the partition", "%1
%2% Used", disk.mount, disk.used*100/disk.size); } else { text = i18nc("Percent used disk space on the partition", "%1: %2
%3% Used", disk.name, disk.mount, disk.used*100/disk.size); } QLabel *label = new QLabel(text, this); label->setAlignment(Qt::AlignHCenter); horizontalLayout->addWidget(label); horizontalLayout->setAlignment(Qt::AlignCenter); volumeLayout->addWidget(map); volumeLayout->addWidget(info); // row (=n/2) column (0 or 1) qobject_cast(layout())->addWidget(volume, layout()->count()/2, layout()->count() % 2); Folder *tree = new Folder(disk.mount.toUtf8().constData()); tree->append("free", disk.free); tree->append("used", disk.used); map->create(tree); //must be done when visible + m_disksFolders.append(tree); connect(map, &RadialMap::Widget::activated, this, &SummaryWidget::activated); } } DiskList::DiskList() { static const QSet ignoredFsTypes = { "tmpfs", "squashfs", "autofs" }; for (const QStorageInfo &storage : QStorageInfo::mountedVolumes()) { if (!storage.isReady() || ignoredFsTypes.contains(storage.fileSystemType())) { continue; } Disk disk; disk.mount = storage.rootPath(); disk.name = storage.name(); disk.size = storage.bytesTotal(); disk.free = storage.bytesFree(); disk.used = disk.size - disk.free; // if something is mounted over same path, last mounted point would be used since only it is currently reachable. (*this)[disk.mount] = disk; } } } #include "summaryWidget.moc" diff --git a/src/summaryWidget.h b/src/summaryWidget.h index 3bfe53f..c46df0a 100644 --- a/src/summaryWidget.h +++ b/src/summaryWidget.h @@ -1,47 +1,52 @@ /*********************************************************************** * Copyright 2003-2004 Max Howell * Copyright 2008-2009 Martin Sandsmark * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . ***********************************************************************/ #ifndef SUMMARYWIDGET_H #define SUMMARYWIDGET_H #include #include +class Folder; + namespace Filelight { class SummaryWidget : public QWidget { Q_OBJECT public: explicit SummaryWidget(QWidget *parent); + ~SummaryWidget(); Q_SIGNALS: void activated(const QUrl&); private: void createDiskMaps(); + + QList m_disksFolders; }; } #endif