From c1bbefb8400dca56544a0731a6d74ef49278482a Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Mon, 7 Jul 2014 15:20:05 +0200 Subject: [PATCH] enable dragging from resources tables --- apps/opencs/model/world/resources.cpp | 9 +++- apps/opencs/model/world/resources.hpp | 9 +++- apps/opencs/model/world/resourcesmanager.cpp | 18 +++++--- apps/opencs/model/world/resourcesmanager.hpp | 4 ++ apps/opencs/model/world/resourcetable.cpp | 45 +++++++++++++++----- 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/apps/opencs/model/world/resources.cpp b/apps/opencs/model/world/resources.cpp index 357661ac3..8e255bc96 100644 --- a/apps/opencs/model/world/resources.cpp +++ b/apps/opencs/model/world/resources.cpp @@ -8,9 +8,9 @@ #include -CSMWorld::Resources::Resources (const std::string& baseDirectory, +CSMWorld::Resources::Resources (const std::string& baseDirectory, UniversalId::Type type, const char * const *extensions) -: mBaseDirectory (baseDirectory) +: mBaseDirectory (baseDirectory), mType (type) { int baseSize = mBaseDirectory.size(); @@ -95,4 +95,9 @@ int CSMWorld::Resources::searchId (const std::string& id) const return -1; return iter->second; +} + +CSMWorld::UniversalId::Type CSMWorld::Resources::getType() const +{ + return mType; } \ No newline at end of file diff --git a/apps/opencs/model/world/resources.hpp b/apps/opencs/model/world/resources.hpp index 599f9a35b..9c1c76b6f 100644 --- a/apps/opencs/model/world/resources.hpp +++ b/apps/opencs/model/world/resources.hpp @@ -5,6 +5,8 @@ #include #include +#include "universalid.hpp" + namespace CSMWorld { class Resources @@ -12,10 +14,13 @@ namespace CSMWorld std::map mIndex; std::vector mFiles; std::string mBaseDirectory; + UniversalId::Type mType; public: - Resources (const std::string& baseDirectory, const char * const *extensions = 0); + /// \param type Type of resources in this table. + Resources (const std::string& baseDirectory, UniversalId::Type type, + const char * const *extensions = 0); int getSize() const; @@ -24,6 +29,8 @@ namespace CSMWorld int getIndex (const std::string& id) const; int searchId (const std::string& id) const; + + UniversalId::Type getType() const; }; } diff --git a/apps/opencs/model/world/resourcesmanager.cpp b/apps/opencs/model/world/resourcesmanager.cpp index 7c782e7c5..5692d30ac 100644 --- a/apps/opencs/model/world/resourcesmanager.cpp +++ b/apps/opencs/model/world/resourcesmanager.cpp @@ -3,17 +3,21 @@ #include +void CSMWorld::ResourcesManager::addResources (const Resources& resources) +{ + mResources.insert (std::make_pair (resources.getType(), resources)); +} + void CSMWorld::ResourcesManager::listResources() { static const char * const sMeshTypes[] = { "nif", 0 }; - mResources.insert ( - std::make_pair (UniversalId::Type_Mesh, Resources ("meshes", sMeshTypes))); - mResources.insert (std::make_pair (UniversalId::Type_Icon, "icons")); - mResources.insert (std::make_pair (UniversalId::Type_Music, "music")); - mResources.insert (std::make_pair (UniversalId::Type_SoundRes, "sound")); - mResources.insert (std::make_pair (UniversalId::Type_Texture, "textures")); - mResources.insert (std::make_pair (UniversalId::Type_Video, "videos")); + addResources (Resources ("meshes", UniversalId::Type_Mesh, sMeshTypes)); + addResources (Resources ("icons", UniversalId::Type_Icon)); + addResources (Resources ("music", UniversalId::Type_Music)); + addResources (Resources ("sound", UniversalId::Type_SoundRes)); + addResources (Resources ("textures", UniversalId::Type_Texture)); + addResources (Resources ("videos", UniversalId::Type_Video)); } const CSMWorld::Resources& CSMWorld::ResourcesManager::get (UniversalId::Type type) const diff --git a/apps/opencs/model/world/resourcesmanager.hpp b/apps/opencs/model/world/resourcesmanager.hpp index e3bbd8a2a..77f210c47 100644 --- a/apps/opencs/model/world/resourcesmanager.hpp +++ b/apps/opencs/model/world/resourcesmanager.hpp @@ -12,6 +12,10 @@ namespace CSMWorld { std::map mResources; + private: + + void addResources (const Resources& resources); + public: /// Ask OGRE for a list of available resources. diff --git a/apps/opencs/model/world/resourcetable.cpp b/apps/opencs/model/world/resourcetable.cpp index f46ac1dc9..167b48436 100644 --- a/apps/opencs/model/world/resourcetable.cpp +++ b/apps/opencs/model/world/resourcetable.cpp @@ -26,7 +26,7 @@ int CSMWorld::ResourceTable::columnCount (const QModelIndex & parent) const if (parent.isValid()) return 0; - return 1; + return 2; // ID, type } QVariant CSMWorld::ResourceTable::data (const QModelIndex & index, int role) const @@ -34,10 +34,13 @@ QVariant CSMWorld::ResourceTable::data (const QModelIndex & index, int role) co if (role!=Qt::DisplayRole) return QVariant(); - if (index.column()!=0) - throw std::logic_error ("Invalid column in resource table"); + if (index.column()==0) + return QString::fromUtf8 (mResources->getId (index.row()).c_str()); - return QString::fromUtf8 (mResources->getId (index.row()).c_str()); + if (index.column()==1) + return static_cast (mResources->getType()); + + throw std::logic_error ("Invalid column in resource table"); } QVariant CSMWorld::ResourceTable::headerData (int section, Qt::Orientation orientation, @@ -46,14 +49,33 @@ QVariant CSMWorld::ResourceTable::headerData (int section, Qt::Orientation orien if (orientation==Qt::Vertical) return QVariant(); - if (role==Qt::DisplayRole) - return "ID"; - if (role==ColumnBase::Role_Flags) return ColumnBase::Flag_Table; - if (role==ColumnBase::Role_Display) - return ColumnBase::Display_String; + switch (section) + { + case 0: + + if (role==Qt::DisplayRole) + return Columns::getName (Columns::ColumnId_Id).c_str(); + + if (role==ColumnBase::Role_Display) + return ColumnBase::Display_String; + + break; + + case 1: + + if (role==Qt::DisplayRole) + return Columns::getName (Columns::ColumnId_RecordType).c_str(); + + if (role==ColumnBase::Role_Display) + return ColumnBase::Display_Integer; + + break; + } + + // xxx return QVariant(); } @@ -78,7 +100,7 @@ QModelIndex CSMWorld::ResourceTable::index (int row, int column, const QModelInd if (row<0 || row>=mResources->getSize()) return QModelIndex(); - if (column!=0) + if (column<0 || column>1) return QModelIndex(); return createIndex (row, column); @@ -99,6 +121,9 @@ int CSMWorld::ResourceTable::searchColumnIndex (Columns::ColumnId id) const if (id==Columns::ColumnId_Id) return 0; + if (id==Columns::ColumnId_RecordType) + return 1; + return -1; }