Warning, file /sdk/cervisia/updateview_visitors.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-2008 André Wöbbeking <Woebbeking@kde.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  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 
0019 #include "updateview_visitors.h"
0020 
0021 #include "updateview_items.h"
0022 
0023 ApplyFilterVisitor::ApplyFilterVisitor(UpdateView::Filter filter)
0024     : m_filter(filter)
0025 {
0026 }
0027 
0028 void ApplyFilterVisitor::preVisit(UpdateDirItem *item)
0029 {
0030     // as QTreeWidgetItem::setHidden() is recursive we have to make
0031     // this UpdateDirItem visible first and later we can make it invisible
0032     item->setHidden(false);
0033 
0034     // assume that this item is invisible and correct it later
0035     // (see markAllParentsAsVisible())
0036     m_invisibleDirItems.insert(item);
0037 }
0038 
0039 void ApplyFilterVisitor::postVisit(UpdateDirItem *item)
0040 {
0041     // a UpdateDirItem is visible if
0042     // - it has visible children
0043     // - it is not opened
0044     // - empty directories are not hidden
0045     // - it has no parent (top level item)
0046     const bool visible(!m_invisibleDirItems.count(item) || !item->wasScanned() || !(m_filter & UpdateView::NoEmptyDirectories) || !item->parent());
0047 
0048     // only set invisible as QTreeWidgetItem::setHidden() is recursive
0049     // and so maybe overrides the state applied by the filter
0050     if (visible) {
0051         markAllParentsAsVisible(item);
0052     } else {
0053         item->setHidden(true);
0054     }
0055 }
0056 
0057 void ApplyFilterVisitor::visit(UpdateFileItem *item)
0058 {
0059     const bool visible(item->applyFilter(m_filter));
0060     if (visible) {
0061         markAllParentsAsVisible(item);
0062     }
0063 }
0064 
0065 void ApplyFilterVisitor::markAllParentsAsVisible(UpdateItem *item)
0066 {
0067     while ((item = static_cast<UpdateDirItem *>(item->parent()))) {
0068         auto it = m_invisibleDirItems.find(item);
0069         if (it != m_invisibleDirItems.end()) {
0070             m_invisibleDirItems.erase(it);
0071         } else {
0072             // if this item isn't in the map anymore all parents
0073             // are already removed too
0074             break;
0075         }
0076     }
0077 }