mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 02:50:04 +00:00 
			
		
		
		
	Radar product model cleanup
This commit is contained in:
		
							parent
							
								
									254fbbeb67
								
							
						
					
					
						commit
						782d61e5f0
					
				
					 3 changed files with 67 additions and 81 deletions
				
			
		|  | @ -22,6 +22,8 @@ public: | |||
|    explicit RadarProductModelImpl(RadarProductModel* self); | ||||
|    ~RadarProductModelImpl() = default; | ||||
| 
 | ||||
|    void AppendRow(TreeItem* parent, TreeItem* child); | ||||
| 
 | ||||
|    RadarProductModel*        self_; | ||||
|    std::shared_ptr<TreeItem> rootItem_; | ||||
| }; | ||||
|  | @ -37,6 +39,18 @@ const std::shared_ptr<TreeItem> RadarProductModel::root_item() const | |||
|    return p->rootItem_; | ||||
| } | ||||
| 
 | ||||
| void RadarProductModelImpl::AppendRow(TreeItem* parent, TreeItem* child) | ||||
| { | ||||
|    const QModelIndex parentIndex = self_->createIndex(parent->row(), 0, parent); | ||||
|    const int         childCount  = parent->child_count(); | ||||
|    const int         first       = childCount; | ||||
|    const int         last        = childCount; | ||||
| 
 | ||||
|    self_->beginInsertRows(parentIndex, first, last); | ||||
|    parent->AppendChild(child); | ||||
|    self_->endInsertRows(); | ||||
| } | ||||
| 
 | ||||
| RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : | ||||
|     self_ {self}, | ||||
|     rootItem_ {std::make_shared<TreeItem>( | ||||
|  | @ -57,16 +71,8 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : | |||
| 
 | ||||
|          if (radarSiteItem == nullptr) | ||||
|          { | ||||
|             const QModelIndex rootIndex = | ||||
|                self_->createIndex(rootItem_->row(), 0, rootItem_.get()); | ||||
|             const int rootChildren = rootItem_->child_count(); | ||||
| 
 | ||||
|             self_->beginInsertRows(rootIndex, rootChildren, rootChildren); | ||||
| 
 | ||||
|             radarSiteItem = new TreeItem({radarSiteName}); | ||||
|             rootItem_->AppendChild(radarSiteItem); | ||||
| 
 | ||||
|             self_->endInsertRows(); | ||||
|             AppendRow(rootItem_.get(), radarSiteItem); | ||||
|          } | ||||
| 
 | ||||
|          connect( | ||||
|  | @ -86,17 +92,8 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : | |||
|                if (groupItem == nullptr) | ||||
|                { | ||||
|                   // Existing group item was not found, create it
 | ||||
|                   const QModelIndex radarSiteIndex = | ||||
|                      self_->createIndex(radarSiteItem->row(), 0, radarSiteItem); | ||||
|                   const int radarSiteChildren = radarSiteItem->child_count(); | ||||
| 
 | ||||
|                   self_->beginInsertRows( | ||||
|                      radarSiteIndex, radarSiteChildren, radarSiteChildren); | ||||
| 
 | ||||
|                   groupItem = new TreeItem({groupName}); | ||||
|                   radarSiteItem->AppendChild(groupItem); | ||||
| 
 | ||||
|                   self_->endInsertRows(); | ||||
|                   AppendRow(radarSiteItem, groupItem); | ||||
|                } | ||||
| 
 | ||||
|                TreeItem* productItem = nullptr; | ||||
|  | @ -115,32 +112,15 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : | |||
|                   if (productItem == nullptr) | ||||
|                   { | ||||
|                      // Existing product item was not found, create it
 | ||||
|                      const QModelIndex groupItemIndex = | ||||
|                         self_->createIndex(groupItem->row(), 0, groupItem); | ||||
|                      const int groupItemChildren = groupItem->child_count(); | ||||
| 
 | ||||
|                      self_->beginInsertRows( | ||||
|                         groupItemIndex, groupItemChildren, groupItemChildren); | ||||
| 
 | ||||
|                      productItem = new TreeItem({productName}); | ||||
|                      groupItem->AppendChild(productItem); | ||||
| 
 | ||||
|                      self_->endInsertRows(); | ||||
|                      AppendRow(groupItem, productItem); | ||||
|                   } | ||||
|                } | ||||
| 
 | ||||
|                // Create leaf item for product time
 | ||||
|                const QModelIndex productItemIndex = | ||||
|                   self_->createIndex(productItem->row(), 0, productItem); | ||||
|                const int productItemChildren = productItem->child_count(); | ||||
| 
 | ||||
|                self_->beginInsertRows( | ||||
|                   productItemIndex, productItemChildren, productItemChildren); | ||||
| 
 | ||||
|                productItem->AppendChild(new TreeItem { | ||||
|                   QString::fromStdString(util::TimeString(latestTime))}); | ||||
| 
 | ||||
|                self_->endInsertRows(); | ||||
|                AppendRow(productItem, | ||||
|                          new TreeItem {QString::fromStdString( | ||||
|                             util::TimeString(latestTime))}); | ||||
|             }, | ||||
|             Qt::QueuedConnection); | ||||
|       }); | ||||
|  |  | |||
|  | @ -12,46 +12,30 @@ static const std::string logPrefix_ = "scwx::qt::model::tree_model"; | |||
| class TreeModelImpl | ||||
| { | ||||
| public: | ||||
|    explicit TreeModelImpl() = default; | ||||
|    ~TreeModelImpl()         = default; | ||||
|    explicit TreeModelImpl(TreeModel* self) : self_ {self} {}; | ||||
|    ~TreeModelImpl() = default; | ||||
| 
 | ||||
|    const TreeItem* item(const QModelIndex& index) const; | ||||
|    TreeItem*       item(const QModelIndex& index); | ||||
| 
 | ||||
|    TreeModel* self_; | ||||
| }; | ||||
| 
 | ||||
| TreeModel::TreeModel(QObject* parent) : | ||||
|     QAbstractItemModel(parent), p(std::make_unique<TreeModelImpl>()) | ||||
|     QAbstractItemModel(parent), p(std::make_unique<TreeModelImpl>(this)) | ||||
| { | ||||
| } | ||||
| TreeModel::~TreeModel() = default; | ||||
| 
 | ||||
| int TreeModel::rowCount(const QModelIndex& parent) const | ||||
| { | ||||
|    const TreeItem* parentItem; | ||||
| 
 | ||||
|    if (parent.isValid()) | ||||
|    { | ||||
|       parentItem = static_cast<const TreeItem*>(parent.constInternalPointer()); | ||||
|    } | ||||
|    else | ||||
|    { | ||||
|       parentItem = root_item().get(); | ||||
|    } | ||||
| 
 | ||||
|    return parentItem->child_count(); | ||||
|    const TreeItem* parentItem = p->item(parent); | ||||
|    return parentItem ? parentItem->child_count() : 0; | ||||
| } | ||||
| 
 | ||||
| int TreeModel::columnCount(const QModelIndex& parent) const | ||||
| int TreeModel::columnCount(const QModelIndex& /* parent */) const | ||||
| { | ||||
|    const TreeItem* parentItem; | ||||
| 
 | ||||
|    if (parent.isValid()) | ||||
|    { | ||||
|       parentItem = static_cast<const TreeItem*>(parent.constInternalPointer()); | ||||
|    } | ||||
|    else | ||||
|    { | ||||
|       parentItem = root_item().get(); | ||||
|    } | ||||
| 
 | ||||
|    return parentItem->column_count(); | ||||
|    return root_item()->column_count(); | ||||
| } | ||||
| 
 | ||||
| QVariant TreeModel::data(const QModelIndex& index, int role) const | ||||
|  | @ -61,7 +45,7 @@ QVariant TreeModel::data(const QModelIndex& index, int role) const | |||
|       return QVariant(); | ||||
|    } | ||||
| 
 | ||||
|    const TreeItem* item = static_cast<const TreeItem*>(index.internalPointer()); | ||||
|    const TreeItem* item = p->item(index); | ||||
| 
 | ||||
|    return item->data(index.column()); | ||||
| } | ||||
|  | @ -96,20 +80,15 @@ TreeModel::headerData(int section, Qt::Orientation orientation, int role) const | |||
| QModelIndex | ||||
| TreeModel::index(int row, int column, const QModelIndex& parent) const | ||||
| { | ||||
|    if (!hasIndex(row, column, parent)) | ||||
|    if (parent.isValid() && parent.column() != 0) | ||||
|    { | ||||
|       return QModelIndex(); | ||||
|    } | ||||
| 
 | ||||
|    const TreeItem* parentItem; | ||||
| 
 | ||||
|    if (!parent.isValid()) | ||||
|    const TreeItem* parentItem = p->item(parent); | ||||
|    if (parentItem == nullptr) | ||||
|    { | ||||
|       parentItem = root_item().get(); | ||||
|    } | ||||
|    else | ||||
|    { | ||||
|       parentItem = static_cast<const TreeItem*>(parent.constInternalPointer()); | ||||
|       return QModelIndex(); | ||||
|    } | ||||
| 
 | ||||
|    const TreeItem* childItem = parentItem->child(row); | ||||
|  | @ -128,9 +107,8 @@ QModelIndex TreeModel::parent(const QModelIndex& index) const | |||
|       return QModelIndex(); | ||||
|    } | ||||
| 
 | ||||
|    const TreeItem* childItem = | ||||
|       static_cast<const TreeItem*>(index.constInternalPointer()); | ||||
|    const TreeItem* parentItem = childItem->parent_item(); | ||||
|    const TreeItem* childItem  = p->item(index); | ||||
|    const TreeItem* parentItem = childItem ? childItem->parent_item() : nullptr; | ||||
| 
 | ||||
|    if (parentItem == root_item().get() || parentItem == nullptr) | ||||
|    { | ||||
|  | @ -140,6 +118,33 @@ QModelIndex TreeModel::parent(const QModelIndex& index) const | |||
|    return createIndex(parentItem->row(), 0, parentItem); | ||||
| } | ||||
| 
 | ||||
| const TreeItem* TreeModelImpl::item(const QModelIndex& index) const | ||||
| { | ||||
|    if (index.isValid()) | ||||
|    { | ||||
|       const TreeItem* item = | ||||
|          static_cast<const TreeItem*>(index.constInternalPointer()); | ||||
|       if (item != nullptr) | ||||
|       { | ||||
|          return item; | ||||
|       } | ||||
|    } | ||||
|    return self_->root_item().get(); | ||||
| } | ||||
| 
 | ||||
| TreeItem* TreeModelImpl::item(const QModelIndex& index) | ||||
| { | ||||
|    if (index.isValid()) | ||||
|    { | ||||
|       TreeItem* item = static_cast<TreeItem*>(index.internalPointer()); | ||||
|       if (item != nullptr) | ||||
|       { | ||||
|          return item; | ||||
|       } | ||||
|    } | ||||
|    return self_->root_item().get(); | ||||
| } | ||||
| 
 | ||||
| } // namespace model
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
|  |  | |||
|  | @ -37,6 +37,7 @@ protected: | |||
|    virtual const std::shared_ptr<TreeItem> root_item() const = 0; | ||||
| 
 | ||||
| private: | ||||
|    friend class TreeModelImpl; | ||||
|    std::unique_ptr<TreeModelImpl> p; | ||||
| }; | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat