Changeset 14559


Ignore:
Timestamp:
Aug 16, 2015, 10:07:09 PM (7 years ago)
Author:
mikedld
Message:

Simplify and speed up actions connected to file tree popup menu

Location:
trunk/qt
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/FileTreeItem.cc

    r14557 r14559  
    9595  QVariant value;
    9696
    97   if (column == FileTreeModel::COL_FILE_INDEX)
    98     {
    99       value.setValue (myFileIndex);
    100     }
    101   else if (role == Qt::EditRole)
    102     {
    103       if (column == 0)
    104         value.setValue (name());
    105     }
    106   else if ((role == Qt::TextAlignmentRole) && column == FileTreeModel::COL_SIZE)
    107     {
    108       value = Qt::AlignRight + Qt::AlignVCenter;
    109     }
    110   else if (role == Qt::DisplayRole || role == FileTreeModel::SortRole)
    111     {
    112       switch(column)
    113         {
    114           case FileTreeModel::COL_NAME:
    115             value.setValue (name());
    116             break;
    117 
    118           case FileTreeModel::COL_SIZE:
    119             if (role == Qt::DisplayRole)
    120               value.setValue (sizeString());
     97  switch (role)
     98    {
     99      case FileTreeModel::FileIndexRole:
     100        value.setValue (myFileIndex);
     101        break;
     102
     103      case FileTreeModel::WantedRole:
     104        value.setValue (isSubtreeWanted ());
     105        break;
     106
     107      case FileTreeModel::CompleteRole:
     108        value.setValue (isComplete ());
     109        break;
     110
     111      case Qt::EditRole:
     112        if (column == FileTreeModel::COL_NAME)
     113          value.setValue (name ());
     114        break;
     115
     116      case Qt::TextAlignmentRole:
     117        if (column == FileTreeModel::COL_SIZE)
     118          value = Qt::AlignRight + Qt::AlignVCenter;
     119        break;
     120
     121      case Qt::DisplayRole:
     122      case FileTreeModel::SortRole:
     123        switch (column)
     124          {
     125            case FileTreeModel::COL_NAME:
     126              value.setValue (name ());
     127              break;
     128
     129            case FileTreeModel::COL_SIZE:
     130              if (role == Qt::DisplayRole)
     131                value.setValue (sizeString ());
     132              else
     133                value.setValue<quint64> (size ());
     134              break;
     135
     136            case FileTreeModel::COL_PROGRESS:
     137              value.setValue (progress ());
     138              break;
     139
     140            case FileTreeModel::COL_WANTED:
     141              value.setValue (isSubtreeWanted ());
     142              break;
     143
     144            case FileTreeModel::COL_PRIORITY:
     145              if (role == Qt::DisplayRole)
     146                value.setValue (priorityString ());
     147              else
     148                value.setValue (priority ());
     149              break;
     150          }
     151        break;
     152
     153      case Qt::DecorationRole:
     154        if (column == FileTreeModel::COL_NAME)
     155          {
     156            if (myFileIndex < 0)
     157              value = qApp->style ()->standardIcon (QStyle::SP_DirOpenIcon);
    121158            else
    122               value.setValue<quint64> (size ());
    123             break;
    124 
    125           case FileTreeModel::COL_PROGRESS:
    126             value.setValue (progress());
    127             break;
    128 
    129           case FileTreeModel::COL_WANTED:
    130             value.setValue (isSubtreeWanted());
    131             break;
    132 
    133           case FileTreeModel::COL_PRIORITY:
    134             if (role == Qt::DisplayRole)
    135               value.setValue (priorityString());
    136             else
    137               value.setValue (priority ());
    138             break;
    139         }
    140     }
    141   else if (role == Qt::DecorationRole && column == FileTreeModel::COL_NAME)
    142     {
    143       if (childCount () > 0)
    144         value = qApp->style ()->standardIcon (QStyle::SP_DirOpenIcon);
    145       else
    146         value = Utils::guessMimeIcon (name ());
     159              value = Utils::guessMimeIcon (name ());
     160          }
     161        break;
    147162    }
    148163
  • trunk/qt/FileTreeModel.cc

    r14557 r14559  
    546546
    547547  FileTreeItem * const item = itemFromIndex (index);
    548   if (item->childCount () != 0 || !item->isComplete ())
     548  if (item->fileIndex () < 0 || !item->isComplete ())
    549549    return false;
    550550
  • trunk/qt/FileTreeModel.h

    r14557 r14559  
    2828    {
    2929      COL_NAME,
    30       FIRST_VISIBLE_COLUMN = COL_NAME,
    3130      COL_SIZE,
    3231      COL_PROGRESS,
    3332      COL_WANTED,
    3433      COL_PRIORITY,
    35       LAST_VISIBLE_COLUMN = COL_PRIORITY,
    3634
    37       COL_FILE_INDEX,
    3835      NUM_COLUMNS
    3936    };
     
    4138    enum Role
    4239    {
    43       SortRole = Qt::UserRole
     40      SortRole = Qt::UserRole,
     41      FileIndexRole,
     42      WantedRole,
     43      CompleteRole
    4444    };
    4545
  • trunk/qt/FileTreeView.cc

    r14557 r14559  
    4242  sortByColumn (FileTreeModel::COL_NAME, Qt::AscendingOrder);
    4343
    44   for (int i=0; i<FileTreeModel::NUM_COLUMNS; ++i)
    45     setColumnHidden (i, (i<FileTreeModel::FIRST_VISIBLE_COLUMN) || (FileTreeModel::LAST_VISIBLE_COLUMN<i));
    46 
    4744  connect (this, SIGNAL(clicked(QModelIndex)),
    4845           this, SLOT(onClicked(QModelIndex)));
     
    8178
    8279  int left = event->size ().width () - 1;
    83   for (int column = FileTreeModel::FIRST_VISIBLE_COLUMN; column <= FileTreeModel::LAST_VISIBLE_COLUMN; ++column)
     80  for (int column = 0; column < FileTreeModel::NUM_COLUMNS; ++column)
    8481    {
    8582      if (column == FileTreeModel::COL_NAME)
    86         continue;
    87       if (isColumnHidden (column))
    8883        continue;
    8984
     
    259254FileTreeView::onlyCheckSelectedItems ()
    260255{
    261   const QModelIndex rootIndex = myModel->index (0, FileTreeModel::COL_WANTED);
     256  const QModelIndex rootIndex = myModel->index (0, 0);
    262257  if (!rootIndex.isValid ())
    263258    return;
    264259
    265   QModelIndexList wantedIndices = selectedSourceRows (FileTreeModel::COL_WANTED);
     260  QModelIndexList wantedIndices = selectedSourceRows ();
    266261  myModel->setWanted (wantedIndices, true);
    267262
     
    272267    {
    273268      for (QModelIndex p = i.parent (); p.isValid (); p = p.parent ())
    274         wantedIndicesParents.insert (p.sibling (p.row (), FileTreeModel::COL_WANTED));
     269        wantedIndicesParents.insert (p);
    275270    }
    276271
     
    286281      for (int i = 0, count = myModel->rowCount (parentIndex); i < count; ++i)
    287282        {
    288           const QModelIndex childIndex = parentIndex.child (i, FileTreeModel::COL_WANTED);
    289           const int childCheckState = childIndex.data ().toInt ();
     283          const QModelIndex childIndex = parentIndex.child (i, 0);
     284          const int childCheckState = childIndex.data (FileTreeModel::WantedRole).toInt ();
    290285          if (childCheckState == Qt::Unchecked || qBinaryFind (wantedIndices, childIndex) != wantedIndices.end ())
    291286            continue;
    292287
    293           if (childCheckState == Qt::Checked &&
    294               childIndex.sibling (childIndex.row (), FileTreeModel::COL_FILE_INDEX).data ().toInt () >= 0)
     288          if (childCheckState == Qt::Checked && childIndex.data (FileTreeModel::FileIndexRole).toInt () >= 0)
    295289            {
    296290              unwantedIndices << childIndex;
     
    334328  assert (myContextMenu != nullptr);
    335329
    336   const QModelIndexList selectedRows = selectionModel ()->selectedRows (FileTreeModel::COL_WANTED);
    337 
    338   QSet<int> checkStates;
    339   for (const QModelIndex& i: selectedRows)
    340     {
    341       checkStates.insert (i.data ().toInt ());
    342       if (checkStates.size() == 3)
    343         break;
    344     }
     330  const QModelIndexList selectedRows = selectionModel ()->selectedRows ();
     331  const Qt::CheckState checkState = getCumulativeCheckState (selectedRows);
    345332
    346333  const bool haveSelection = !selectedRows.isEmpty ();
    347334  const bool haveSingleSelection = selectedRows.size() == 1;
    348   const bool haveUnchecked = checkStates.contains (Qt::Unchecked) || checkStates.contains (Qt::PartiallyChecked);
    349   const bool haveChecked = checkStates.contains (Qt::Checked) || checkStates.contains (Qt::PartiallyChecked);
     335  const bool haveUnchecked = checkState == Qt::Unchecked || checkState == Qt::PartiallyChecked;
     336  const bool haveChecked = checkState == Qt::Checked || checkState == Qt::PartiallyChecked;
    350337
    351338  myCheckSelectedAction->setEnabled (haveUnchecked);
     
    353340  myOnlyCheckSelectedAction->setEnabled (haveSelection);
    354341  myPriorityMenu->setEnabled (haveSelection);
    355   myOpenAction->setEnabled (haveSingleSelection && myProxy->rowCount (selectedRows.first ()) == 0);
     342  myOpenAction->setEnabled (haveSingleSelection &&
     343    selectedRows.first ().data (FileTreeModel::FileIndexRole).toInt () >= 0 &&
     344    selectedRows.first ().data (FileTreeModel::CompleteRole).toBool ());
    356345  myRenameAction->setEnabled (haveSingleSelection);
    357346}
     
    362351  myContextMenu = new QMenu (this);
    363352
    364   myCheckSelectedAction = myContextMenu->addAction (tr ("Check selected"), this, SLOT (checkSelectedItems ()));
    365   myUncheckSelectedAction = myContextMenu->addAction (tr ("Uncheck selected"), this, SLOT (uncheckSelectedItems ()));
    366   myOnlyCheckSelectedAction = myContextMenu->addAction (tr ("Only check selected"), this, SLOT (onlyCheckSelectedItems ()));
     353  myCheckSelectedAction = myContextMenu->addAction (tr ("Check Selected"), this, SLOT (checkSelectedItems ()));
     354  myUncheckSelectedAction = myContextMenu->addAction (tr ("Uncheck Selected"), this, SLOT (uncheckSelectedItems ()));
     355  myOnlyCheckSelectedAction = myContextMenu->addAction (tr ("Only Check Selected"), this, SLOT (onlyCheckSelectedItems ()));
    367356
    368357  myContextMenu->addSeparator ();
     
    393382  return indices;
    394383}
     384
     385Qt::CheckState
     386FileTreeView::getCumulativeCheckState (const QModelIndexList& indices)
     387{
     388  bool haveChecked = false, haveUnchecked = false;
     389
     390  for (const QModelIndex& i: indices)
     391    {
     392      switch (i.data (FileTreeModel::WantedRole).toInt ())
     393        {
     394          case Qt::Checked:
     395            haveChecked = true;
     396            break;
     397          case Qt::Unchecked:
     398            haveUnchecked = true;
     399            break;
     400          case Qt::PartiallyChecked:
     401            return Qt::PartiallyChecked;
     402        }
     403
     404      if (haveChecked && haveUnchecked)
     405        return Qt::PartiallyChecked;
     406    }
     407
     408  return haveChecked ? Qt::Checked : Qt::Unchecked;
     409}
  • trunk/qt/FileTreeView.h

    r14557 r14559  
    6262
    6363    void refreshContextMenuActionsSensitivity ();
    64     QModelIndexList selectedSourceRows (int column = 0) const;
    6564
    6665  private:
    6766    void initContextMenu ();
     67    QModelIndexList selectedSourceRows (int column = 0) const;
     68
     69    static Qt::CheckState getCumulativeCheckState (const QModelIndexList& indices);
    6870
    6971  private:
Note: See TracChangeset for help on using the changeset viewer.