SHARE
TWEET

Untitled

a guest Jul 21st, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TreeHeader : public QHeaderView
  2. {
  3. public:
  4.     TreeHeader(QWidget *parent = nullptr) noexcept;
  5.  
  6.     QAction *getModeAction(const QString &sActionTitle) const;
  7.  
  8.     void addModeAction(QAction *action, eTreeMode mode, QAction *before = nullptr);
  9.  
  10. protected:
  11.     /// Override the context menu event
  12.     void contextMenuEvent(QContextMenuEvent *event) override;
  13.  
  14.     /// The context menu
  15.     QMenu *m_pContextMenu = nullptr;
  16.  
  17. private:
  18.     /// Setups connections and necessary data for the given action
  19.     void setupAction(QAction *action, eTreeMode eMode) const;
  20.  
  21. }; // class TreeHeader
  22.  
  23. TreeHeader::TreeHeader(QWidget *parent /* = nullptr */) noexcept
  24.     : QHeaderView(Qt::Horizontal, parent)
  25.     , m_pContextMenu(new QMenu(this))
  26. {
  27.     std::vector<std::pair<eTreeMode, QString>> mapModeToName;
  28.     mapModeToName.emplace_back(eTreeMode::Mode1, QString("By %1").arg(mode1));
  29.     mapModeToName.emplace_back(eTreeMode::Mode2, QString("By %1").arg(mode2));
  30.  
  31.     // create the default actions
  32.     for (const auto &pair : mapModeToName)
  33.     {
  34.         // add the action to the menu and setup necessary properties to it
  35.         setupAction(m_pContextMenu->addAction(pair.second), pair.first);
  36.     }
  37.  
  38.     setTextElideMode(Qt::ElideRight);
  39.     setStretchLastSection(true);
  40. }
  41.  
  42. QAction *TreeHeader::getModeAction(const QString &sActionTitle) const
  43. {
  44.     for (const auto pAction : m_pContextMenu->actions())
  45.     {
  46.         if (pAction->text() == sActionTitle)
  47.             return pAction;
  48.     }
  49.     return nullptr;
  50. }
  51.  
  52. void TreeHeader::addModeAction(QAction *action, eTreeMode mode, QAction *before /* = nullptr */)
  53. {
  54.     if (!action) return;
  55.  
  56.     // add the action at appropriate position
  57.     if (before)
  58.         m_pContextMenu->insertAction(before, action);
  59.     else
  60.         m_pContextMenu->addAction(action);
  61.  
  62.     // setup necessary properties to the action
  63.     setupAction(action, mode);
  64. }
  65.  
  66. void TreeHeader::contextMenuEvent(QContextMenuEvent *event)
  67. {
  68.     if (auto pTree = static_cast<TreeView *>(parentWidget()))
  69.     {
  70.         // make sure we have a populated tree
  71.         if (!(pTree->getTreeModel() && pTree->getTreeModel()->hasChildren())) return;
  72.  
  73.         // show the context menu only for the first column which specifies the grouping mode
  74.         if (logicalIndexAt(event->pos()) == 0)
  75.         {
  76.             // get the current mode
  77.             auto eMode = pTree->getMode();
  78.  
  79.             // set correct checked states of the actions
  80.             for (const auto pAction : m_pContextMenu->actions())
  81.                 pAction->setChecked(eMode == static_cast<eTreeMode>(pAction->data().toInt()));
  82.  
  83.             // and show the menu
  84.             m_pContextMenu->exec(event->globalPos());
  85.         }
  86.     }
  87. }
  88.  
  89. void TreeHeader::setupAction(QAction *action, eTreeMode eMode) const
  90. {
  91.     // make the action checkable and check it, if necessary
  92.     action->setCheckable(true);
  93.  
  94.     // set the action's checked state and make necessary connections
  95.     if (auto pTree = dynamic_cast<TreeView *>(parentWidget()))
  96.     {
  97.         action->setChecked(pTree->getMode() == eMode);
  98.  
  99.         // set up a connection to process to mode change
  100.         if (auto pView = dynamic_cast<TreeViewContainer *>(pTree->parentWidget()))
  101.         {
  102.             // setup a connection to process mode change
  103.             connect(action, &QAction::triggered, pView, &TreeViewContainer::onModeChanged);
  104.         }
  105.     }
  106.  
  107.     // set the mode as data
  108.     action->setData(eMode);
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top