ClassView: Dissolve the internal 'Utils' class

It's regularly getting in my way, and it's rather a namespace anyway.

Also, move the functions that are only used once closer to their
place of use.

Change-Id: I97951aae1b69c04f391afbdd1b491a8a9173a977
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2019-11-15 18:42:11 +01:00
parent 3ecf3d9575
commit 3c6c61d1c8
8 changed files with 138 additions and 227 deletions

View File

@@ -437,7 +437,7 @@ void Manager::gotoLocation(const QString &fileName, int line, int column)
void Manager::gotoLocations(const QList<QVariant> &list) void Manager::gotoLocations(const QList<QVariant> &list)
{ {
QSet<SymbolLocation> locations = Utils::roleToLocations(list); QSet<SymbolLocation> locations = Internal::roleToLocations(list);
if (locations.size() == 0) if (locations.size() == 0)
return; return;

View File

@@ -315,8 +315,8 @@ void NavigationWidget::fetchExpandedItems(QStandardItem *item, const QStandardIt
QStandardItem *itemChild = item->child(itemIndex); QStandardItem *itemChild = item->child(itemIndex);
const QStandardItem *targetChild = target->child(targetIndex); const QStandardItem *targetChild = target->child(targetIndex);
const SymbolInformation &itemInf = Utils::symbolInformationFromItem(itemChild); const SymbolInformation &itemInf = Internal::symbolInformationFromItem(itemChild);
const SymbolInformation &targetInf = Utils::symbolInformationFromItem(targetChild); const SymbolInformation &targetInf = Internal::symbolInformationFromItem(targetChild);
if (itemInf < targetInf) { if (itemInf < targetInf) {
++itemIndex; ++itemIndex;

View File

@@ -58,6 +58,7 @@
enum { debug = false }; enum { debug = false };
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils;
namespace ClassView { namespace ClassView {
namespace Internal { namespace Internal {
@@ -258,7 +259,7 @@ ParserTreeItem::ConstPtr Parser::findItemByRoot(const QStandardItem *item, bool
while (uiList.count() > 0) { while (uiList.count() > 0) {
cur = uiList.last(); cur = uiList.last();
uiList.removeLast(); uiList.removeLast();
const SymbolInformation &inf = Utils::symbolInformationFromItem(cur); const SymbolInformation &inf = Internal::symbolInformationFromItem(cur);
internal = internal->child(inf); internal = internal->child(inf);
if (internal.isNull()) if (internal.isNull())
break; break;
@@ -586,7 +587,7 @@ void Parser::clearCache()
void Parser::setFileList(const QStringList &fileList) void Parser::setFileList(const QStringList &fileList)
{ {
d->fileList = ::Utils::toSet(fileList); d->fileList = Utils::toSet(fileList);
} }
/*! /*!
@@ -632,12 +633,12 @@ void Parser::resetData(const CPlusPlus::Snapshot &snapshot)
d->docLocker.unlock(); d->docLocker.unlock();
// recalculate file list // recalculate file list
::Utils::FilePathList fileList; FilePathList fileList;
// check all projects // check all projects
for (const Project *prj : SessionManager::projects()) for (const Project *prj : SessionManager::projects())
fileList += prj->files(Project::SourceFiles); fileList += prj->files(Project::SourceFiles);
setFileList(::Utils::transform(fileList, &::Utils::FilePath::toString)); setFileList(Utils::transform(fileList, &FilePath::toString));
emit resetDataDone(); emit resetDataDone();
} }
@@ -719,7 +720,7 @@ QStringList Parser::addProjectTree(const ParserTreeItem::Ptr &item, const Projec
if (cit != d->cachedPrjFileLists.constEnd()) { if (cit != d->cachedPrjFileLists.constEnd()) {
fileList = cit.value(); fileList = cit.value();
} else { } else {
fileList = ::Utils::transform(project->files(Project::SourceFiles), &::Utils::FilePath::toString); fileList = Utils::transform(project->files(Project::SourceFiles), &FilePath::toString);
d->cachedPrjFileLists[projectPath] = fileList; d->cachedPrjFileLists[projectPath] = fileList;
} }
if (fileList.count() > 0) { if (fileList.count() > 0) {
@@ -744,7 +745,7 @@ QStringList Parser::getAllFiles(const Project *project)
if (cit != d->cachedPrjFileLists.constEnd()) { if (cit != d->cachedPrjFileLists.constEnd()) {
fileList = cit.value(); fileList = cit.value();
} else { } else {
fileList = ::Utils::transform(project->files(Project::SourceFiles), &::Utils::FilePath::toString); fileList = Utils::transform(project->files(Project::SourceFiles), &FilePath::toString);
d->cachedPrjFileLists[nodePath] = fileList; d->cachedPrjFileLists[nodePath] = fileList;
} }
return fileList; return fileList;

View File

@@ -29,6 +29,8 @@
#include "classviewconstants.h" #include "classviewconstants.h"
#include "classviewutils.h" #include "classviewutils.h"
#include <utils/algorithm.h>
#include <QHash> #include <QHash>
#include <QPair> #include <QPair>
#include <QIcon> #include <QIcon>
@@ -276,6 +278,22 @@ void ParserTreeItem::add(const ParserTreeItem::ConstPtr &target)
} }
} }
/*!
Converts internal location container to QVariant compatible.
\a locations specifies a set of symbol locations.
Returns a list of variant locations that can be added to the data of an
item.
*/
static QList<QVariant> locationsToRole(const QSet<SymbolLocation> &locations)
{
QList<QVariant> locationsVar;
for (const SymbolLocation &loc : locations)
locationsVar.append(QVariant::fromValue(loc));
return locationsVar;
}
/*! /*!
Appends this item to the QStandardIten item \a item. Appends this item to the QStandardIten item \a item.
*/ */
@@ -304,7 +322,10 @@ void ParserTreeItem::convertTo(QStandardItem *item) const
ParserTreeItem::Ptr ptr = cur.value(); ParserTreeItem::Ptr ptr = cur.value();
auto add = new QStandardItem; auto add = new QStandardItem;
Utils::setSymbolInformationToItem(inf, add); add->setData(inf.name(), Constants::SymbolNameRole);
add->setData(inf.type(), Constants::SymbolTypeRole);
add->setData(inf.iconType(), Constants::IconTypeRole);
if (!ptr.isNull()) { if (!ptr.isNull()) {
// icon // icon
add->setIcon(ptr->icon()); add->setIcon(ptr->icon());
@@ -314,8 +335,7 @@ void ParserTreeItem::convertTo(QStandardItem *item) const
add->setFlags(add->flags() | Qt::ItemIsDragEnabled); add->setFlags(add->flags() | Qt::ItemIsDragEnabled);
// locations // locations
add->setData(Utils::locationsToRole(ptr->symbolLocations()), add->setData(locationsToRole(ptr->symbolLocations()), Constants::SymbolLocationsRole);
Constants::SymbolLocationsRole);
} }
item->appendRow(add); item->appendRow(add);
++cur; ++cur;

View File

@@ -24,8 +24,12 @@
****************************************************************************/ ****************************************************************************/
#include "classviewsymbolinformation.h" #include "classviewsymbolinformation.h"
#include "classviewconstants.h"
#include "classviewutils.h" #include "classviewutils.h"
#include <utils/utilsicons.h>
#include <QPair> #include <QPair>
#include <QHash> #include <QHash>
@@ -61,7 +65,46 @@ SymbolInformation::SymbolInformation(const QString &valueName, const QString &va
int SymbolInformation::iconTypeSortOrder() const int SymbolInformation::iconTypeSortOrder() const
{ {
return Utils::iconTypeSortOrder(m_iconType); namespace Icons = Utils::CodeModelIcon;
constexpr int IconSortOrder[] = {
Icons::Namespace,
Icons::Enum,
Icons::Class,
Icons::FuncPublic,
Icons::FuncProtected,
Icons::FuncPrivate,
Icons::FuncPublicStatic,
Icons::FuncProtectedStatic,
Icons::FuncPrivateStatic,
Icons::Signal,
Icons::SlotPublic,
Icons::SlotProtected,
Icons::SlotPrivate,
Icons::VarPublic,
Icons::VarProtected,
Icons::VarPrivate,
Icons::VarPublicStatic,
Icons::VarProtectedStatic,
Icons::VarPrivateStatic,
Icons::Enumerator,
Icons::Keyword,
Icons::Macro,
Icons::Unknown
};
static QHash<int, int> sortOrder;
// initialization
if (sortOrder.isEmpty()) {
for (int i : IconSortOrder)
sortOrder.insert(i, sortOrder.count());
}
// if it is missing - return the same value
if (!sortOrder.contains(m_iconType))
return m_iconType;
return sortOrder[m_iconType];
} }
bool SymbolInformation::operator<(const SymbolInformation &other) const bool SymbolInformation::operator<(const SymbolInformation &other) const

View File

@@ -34,6 +34,59 @@
namespace ClassView { namespace ClassView {
namespace Internal { namespace Internal {
/*!
Moves \a item to \a target (sorted).
*/
static void moveItemToTarget(QStandardItem *item, const QStandardItem *target)
{
if (!item || !target)
return;
int itemIndex = 0;
int targetIndex = 0;
int itemRows = item->rowCount();
int targetRows = target->rowCount();
while (itemIndex < itemRows && targetIndex < targetRows) {
QStandardItem *itemChild = item->child(itemIndex);
const QStandardItem *targetChild = target->child(targetIndex);
const SymbolInformation &itemInf = Internal::symbolInformationFromItem(itemChild);
const SymbolInformation &targetInf = Internal::symbolInformationFromItem(targetChild);
if (itemInf < targetInf) {
item->removeRow(itemIndex);
--itemRows;
} else if (itemInf == targetInf) {
moveItemToTarget(itemChild, targetChild);
++itemIndex;
++targetIndex;
} else {
item->insertRow(itemIndex, targetChild->clone());
moveItemToTarget(item->child(itemIndex), targetChild);
++itemIndex;
++itemRows;
++targetIndex;
}
}
// append
while (targetIndex < targetRows) {
item->appendRow(target->child(targetIndex)->clone());
moveItemToTarget(item->child(itemIndex), target->child(targetIndex));
++itemIndex;
++itemRows;
++targetIndex;
}
// remove end of item
while (itemIndex < itemRows) {
item->removeRow(itemIndex);
--itemRows;
}
}
///////////////////////////////// TreeItemModel ////////////////////////////////// ///////////////////////////////// TreeItemModel //////////////////////////////////
/*! /*!
@@ -66,7 +119,7 @@ QVariant TreeItemModel::data(const QModelIndex &index, int role) const
break; break;
case Qt::ToolTipRole: case Qt::ToolTipRole:
case Qt::DisplayRole: { case Qt::DisplayRole: {
const SymbolInformation &inf = Utils::symbolInformationFromItem(itemFromIndex(index)); const SymbolInformation &inf = Internal::symbolInformationFromItem(itemFromIndex(index));
if (inf.name() == inf.type() || inf.iconType() < 0) if (inf.name() == inf.type() || inf.iconType() < 0)
return inf.name(); return inf.name();
@@ -125,7 +178,7 @@ QMimeData *TreeItemModel::mimeData(const QModelIndexList &indexes) const
auto mimeData = new ::Utils::DropMimeData; auto mimeData = new ::Utils::DropMimeData;
mimeData->setOverrideFileDropAction(Qt::CopyAction); mimeData->setOverrideFileDropAction(Qt::CopyAction);
foreach (const QModelIndex &index, indexes) { foreach (const QModelIndex &index, indexes) {
const QSet<SymbolLocation> locations = Utils::roleToLocations( const QSet<SymbolLocation> locations = Internal::roleToLocations(
data(index, Constants::SymbolLocationsRole).toList()); data(index, Constants::SymbolLocationsRole).toList());
if (locations.isEmpty()) if (locations.isEmpty())
continue; continue;
@@ -147,7 +200,7 @@ void TreeItemModel::moveRootToTarget(const QStandardItem *target)
{ {
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
Utils::moveItemToTarget(invisibleRootItem(), target); moveItemToTarget(invisibleRootItem(), target);
emit layoutChanged(); emit layoutChanged();
} }

View File

@@ -27,69 +27,12 @@
#include "classviewconstants.h" #include "classviewconstants.h"
#include "classviewsymbolinformation.h" #include "classviewsymbolinformation.h"
// needed for the correct sorting order
#include <cplusplus/Icons.h>
#include <QStandardItem> #include <QStandardItem>
#include <QDebug> #include <QDebug>
namespace ClassView { namespace ClassView {
namespace Constants {
/*!
\class Utils
\brief The Utils class provides some common utilities.
*/
//! Default icon sort order
const int IconSortOrder[] = {
Utils::CodeModelIcon::Namespace,
Utils::CodeModelIcon::Enum,
Utils::CodeModelIcon::Class,
Utils::CodeModelIcon::FuncPublic,
Utils::CodeModelIcon::FuncProtected,
Utils::CodeModelIcon::FuncPrivate,
Utils::CodeModelIcon::FuncPublicStatic,
Utils::CodeModelIcon::FuncProtectedStatic,
Utils::CodeModelIcon::FuncPrivateStatic,
Utils::CodeModelIcon::Signal,
Utils::CodeModelIcon::SlotPublic,
Utils::CodeModelIcon::SlotProtected,
Utils::CodeModelIcon::SlotPrivate,
Utils::CodeModelIcon::VarPublic,
Utils::CodeModelIcon::VarProtected,
Utils::CodeModelIcon::VarPrivate,
Utils::CodeModelIcon::VarPublicStatic,
Utils::CodeModelIcon::VarProtectedStatic,
Utils::CodeModelIcon::VarPrivateStatic,
Utils::CodeModelIcon::Enumerator,
Utils::CodeModelIcon::Keyword,
Utils::CodeModelIcon::Macro,
Utils::CodeModelIcon::Unknown
};
} // namespace Constants
namespace Internal { namespace Internal {
Utils::Utils() = default;
/*!
Converts internal location container to QVariant compatible.
\a locations specifies a set of symbol locations.
Returns a list of variant locations that can be added to the data of an
item.
*/
QList<QVariant> Utils::locationsToRole(const QSet<SymbolLocation> &locations)
{
QList<QVariant> locationsVar;
foreach (const SymbolLocation &loc, locations)
locationsVar.append(QVariant::fromValue(loc));
return locationsVar;
}
/*! /*!
Converts QVariant location container to internal. Converts QVariant location container to internal.
\a locationsVar contains a list of variant locations from the data of an \a locationsVar contains a list of variant locations from the data of an
@@ -97,7 +40,7 @@ QList<QVariant> Utils::locationsToRole(const QSet<SymbolLocation> &locations)
Returns a set of symbol locations. Returns a set of symbol locations.
*/ */
QSet<SymbolLocation> Utils::roleToLocations(const QList<QVariant> &locationsVar) QSet<SymbolLocation> roleToLocations(const QList<QVariant> &locationsVar)
{ {
QSet<SymbolLocation> locations; QSet<SymbolLocation> locations;
foreach (const QVariant &loc, locationsVar) { foreach (const QVariant &loc, locationsVar) {
@@ -108,50 +51,12 @@ QSet<SymbolLocation> Utils::roleToLocations(const QList<QVariant> &locationsVar)
return locations; return locations;
} }
/*!
Returns sort order value for the \a icon.
*/
int Utils::iconTypeSortOrder(int icon)
{
static QHash<int, int> sortOrder;
// initialization
if (sortOrder.isEmpty()) {
for (int i : Constants::IconSortOrder)
sortOrder.insert(i, sortOrder.count());
}
// if it is missing - return the same value
if (!sortOrder.contains(icon))
return icon;
return sortOrder[icon];
}
/*!
Sets symbol information specified by \a information to \a item.
\a information provides the name, type, and icon for the item.
Returns the filled item.
*/
QStandardItem *Utils::setSymbolInformationToItem(const SymbolInformation &information,
QStandardItem *item)
{
Q_ASSERT(item);
item->setData(information.name(), Constants::SymbolNameRole);
item->setData(information.type(), Constants::SymbolTypeRole);
item->setData(information.iconType(), Constants::IconTypeRole);
return item;
}
/*! /*!
Returns symbol information for \a item. Returns symbol information for \a item.
*/ */
SymbolInformation Utils::symbolInformationFromItem(const QStandardItem *item) SymbolInformation symbolInformationFromItem(const QStandardItem *item)
{ {
Q_ASSERT(item); Q_ASSERT(item);
@@ -173,98 +78,5 @@ SymbolInformation Utils::symbolInformationFromItem(const QStandardItem *item)
return SymbolInformation(name, type, iconType); return SymbolInformation(name, type, iconType);
} }
/*!
Updates \a item to \a target, so that it is sorted and can be fetched.
*/
void Utils::fetchItemToTarget(QStandardItem *item, const QStandardItem *target)
{
if (!item || !target)
return;
int itemIndex = 0;
int targetIndex = 0;
int itemRows = item->rowCount();
int targetRows = target->rowCount();
while (itemIndex < itemRows && targetIndex < targetRows) {
const QStandardItem *itemChild = item->child(itemIndex);
const QStandardItem *targetChild = target->child(targetIndex);
const SymbolInformation &itemInf = symbolInformationFromItem(itemChild);
const SymbolInformation &targetInf = symbolInformationFromItem(targetChild);
if (itemInf < targetInf) {
++itemIndex;
} else if (itemInf == targetInf) {
++itemIndex;
++targetIndex;
} else {
item->insertRow(itemIndex, targetChild->clone());
++itemIndex;
++itemRows;
++targetIndex;
}
}
// append
while (targetIndex < targetRows) {
item->appendRow(target->child(targetIndex)->clone());
++targetIndex;
}
}
/*!
Moves \a item to \a target (sorted).
*/
void Utils::moveItemToTarget(QStandardItem *item, const QStandardItem *target)
{
if (!item || !target)
return;
int itemIndex = 0;
int targetIndex = 0;
int itemRows = item->rowCount();
int targetRows = target->rowCount();
while (itemIndex < itemRows && targetIndex < targetRows) {
QStandardItem *itemChild = item->child(itemIndex);
const QStandardItem *targetChild = target->child(targetIndex);
const SymbolInformation &itemInf = Utils::symbolInformationFromItem(itemChild);
const SymbolInformation &targetInf = Utils::symbolInformationFromItem(targetChild);
if (itemInf < targetInf) {
item->removeRow(itemIndex);
--itemRows;
} else if (itemInf == targetInf) {
moveItemToTarget(itemChild, targetChild);
++itemIndex;
++targetIndex;
} else {
item->insertRow(itemIndex, targetChild->clone());
moveItemToTarget(item->child(itemIndex), targetChild);
++itemIndex;
++itemRows;
++targetIndex;
}
}
// append
while (targetIndex < targetRows) {
item->appendRow(target->child(targetIndex)->clone());
moveItemToTarget(item->child(itemIndex), target->child(targetIndex));
++itemIndex;
++itemRows;
++targetIndex;
}
// remove end of item
while (itemIndex < itemRows) {
item->removeRow(itemIndex);
--itemRows;
}
}
} // namespace Internal } // namespace Internal
} // namespace ClassView } // namespace ClassView

View File

@@ -27,6 +27,7 @@
#include "classviewsymbollocation.h" #include "classviewsymbollocation.h"
#include "classviewsymbolinformation.h" #include "classviewsymbolinformation.h"
#include <QVariant> #include <QVariant>
#include <QList> #include <QList>
#include <QSet> #include <QSet>
@@ -36,27 +37,8 @@ QT_FORWARD_DECLARE_CLASS(QStandardItem)
namespace ClassView { namespace ClassView {
namespace Internal { namespace Internal {
class Utils QSet<SymbolLocation> roleToLocations(const QList<QVariant> &locations);
{ SymbolInformation symbolInformationFromItem(const QStandardItem *item);
//! Private constructor
Utils();
public:
static QList<QVariant> locationsToRole(const QSet<SymbolLocation> &locations);
static QSet<SymbolLocation> roleToLocations(const QList<QVariant> &locations);
static int iconTypeSortOrder(int iconType);
static SymbolInformation symbolInformationFromItem(const QStandardItem *item);
static QStandardItem *setSymbolInformationToItem(const SymbolInformation &information,
QStandardItem *item);
static void fetchItemToTarget(QStandardItem *item, const QStandardItem *target);
static void moveItemToTarget(QStandardItem *item, const QStandardItem *target);
};
} // namespace Internal } // namespace Internal
} // namespace ClassView } // namespace ClassView