2015-06-10 21:27:11 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
2015-08-10 19:40:58 +00:00
|
|
|
#include <cassert>
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
#include <QHeaderView>
|
2015-08-10 19:40:58 +00:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QQueue>
|
2015-06-10 21:27:11 +00:00
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
2015-08-10 19:40:58 +00:00
|
|
|
#include <libtransmission/transmission.h> // priorities
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "FileTreeDelegate.h"
|
2015-06-15 21:07:46 +00:00
|
|
|
#include "FileTreeItem.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "FileTreeModel.h"
|
|
|
|
#include "FileTreeView.h"
|
2015-06-15 21:07:46 +00:00
|
|
|
#include "Formatter.h"
|
|
|
|
#include "Utils.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2015-08-10 19:40:58 +00:00
|
|
|
#define PRIORITY_KEY "priority"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeView::FileTreeView(QWidget* parent, bool isEditable) :
|
|
|
|
QTreeView(parent),
|
|
|
|
myModel(new FileTreeModel(this, isEditable)),
|
|
|
|
myProxy(new QSortFilterProxyModel(this)),
|
|
|
|
myDelegate(new FileTreeDelegate(this))
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myProxy->setSourceModel(myModel);
|
|
|
|
myProxy->setSortRole(FileTreeModel::SortRole);
|
|
|
|
myProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setModel(myProxy);
|
|
|
|
setItemDelegate(myDelegate);
|
|
|
|
sortByColumn(FileTreeModel::COL_NAME, Qt::AscendingOrder);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(this, SIGNAL(clicked(QModelIndex)), this, SLOT(onClicked(QModelIndex)));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(myModel, SIGNAL(priorityChanged(QSet<int>, int)), this, SIGNAL(priorityChanged(QSet<int>, int)));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(myModel, SIGNAL(wantedChanged(QSet<int>, bool)), this, SIGNAL(wantedChanged(QSet<int>, bool)));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(myModel, SIGNAL(pathEdited(QString, QString)), this, SIGNAL(pathEdited(QString, QString)));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(myModel, SIGNAL(openRequested(QString)), this, SIGNAL(openRequested(QString)));
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeView::onClicked(QModelIndex const& proxyIndex)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const modelIndex = myProxy->mapToSource(proxyIndex);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (modelIndex.column() == FileTreeModel::COL_WANTED)
|
|
|
|
{
|
|
|
|
myModel->twiddleWanted(QModelIndexList() << modelIndex);
|
|
|
|
}
|
|
|
|
else if (modelIndex.column() == FileTreeModel::COL_PRIORITY)
|
|
|
|
{
|
|
|
|
myModel->twiddlePriority(QModelIndexList() << modelIndex);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::resizeEvent(QResizeEvent* event)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::resizeEvent(event);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// this is kind of a hack to get the last four columns be the
|
|
|
|
// right size, and to have the filename column use whatever
|
|
|
|
// space is left over...
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int left = event->size().width() - 1;
|
|
|
|
|
|
|
|
for (int column = 0; column < FileTreeModel::NUM_COLUMNS; ++column)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (column == FileTreeModel::COL_NAME)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int minWidth = 0;
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QStringList itemTexts;
|
|
|
|
|
|
|
|
switch (column)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_SIZE:
|
2015-06-15 21:07:46 +00:00
|
|
|
for (int s = Formatter::B; s <= Formatter::TB; ++s)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
itemTexts << QLatin1String("999.9 ") + Formatter::unitStr(Formatter::MEM, static_cast<Formatter::Size>(s));
|
|
|
|
}
|
|
|
|
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_PROGRESS:
|
|
|
|
itemTexts << QLatin1String(" 100% ");
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_WANTED:
|
2015-06-15 21:07:46 +00:00
|
|
|
minWidth = 20;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_PRIORITY:
|
|
|
|
itemTexts << FileTreeItem::tr("Low") << FileTreeItem::tr("Normal") << FileTreeItem::tr("High") <<
|
|
|
|
FileTreeItem::tr("Mixed");
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int itemWidth = 0;
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QString const& itemText : itemTexts)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
itemWidth = std::max(itemWidth, Utils::measureViewItem(this, itemText));
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const headerText = myModel->headerData(column, Qt::Horizontal).toString();
|
2017-04-19 12:04:45 +00:00
|
|
|
int headerWidth = Utils::measureHeaderItem(this->header(), headerText);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const width = std::max(minWidth, std::max(itemWidth, headerWidth));
|
2017-04-19 12:04:45 +00:00
|
|
|
setColumnWidth(column, width);
|
|
|
|
|
|
|
|
left -= width;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setColumnWidth(FileTreeModel::COL_NAME, std::max(left, 0));
|
2015-06-15 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::keyPressEvent(QKeyEvent* event)
|
2015-06-15 21:07:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (state() != EditingState)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (event->key() == Qt::Key_Space)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// handle using the keyboard to toggle the
|
|
|
|
// wanted/unwanted state or the file priority
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
Qt::KeyboardModifiers const modifiers = event->modifiers();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (modifiers == Qt::NoModifier)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myModel->twiddleWanted(selectedSourceRows());
|
|
|
|
return;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (modifiers == Qt::ShiftModifier)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myModel->twiddlePriority(selectedSourceRows());
|
|
|
|
return;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::keyPressEvent(event);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::mouseDoubleClickEvent(QMouseEvent* event)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const index = currentIndex();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!index.isValid() || index.column() == FileTreeModel::COL_WANTED || index.column() == FileTreeModel::COL_PRIORITY)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (openSelectedItem())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::mouseDoubleClickEvent(event);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::contextMenuEvent(QContextMenuEvent* event)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const rootIndex = myModel->index(0, 0);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!rootIndex.isValid())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myContextMenu == nullptr)
|
|
|
|
{
|
|
|
|
initContextMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
myContextMenu->popup(event->globalPos());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeView::update(FileList const& files, bool updateFields)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const modelWasEmpty = myProxy->rowCount() == 0;
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (TorrentFile const& file : files)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
myModel->addFile(file.index, file.filename, file.wanted, file.priority, file.size, file.have, updateFields);
|
|
|
|
}
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (modelWasEmpty)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// expand up until the item with more than one expandable child
|
|
|
|
for (QModelIndex index = myProxy->index(0, 0); index.isValid();)
|
2015-07-12 20:48:54 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const oldIndex = index;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
expand(oldIndex);
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
index = QModelIndex();
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = 0, count = myProxy->rowCount(oldIndex); i < count; ++i)
|
2015-07-12 20:48:54 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const newIndex = myProxy->index(i, 0, oldIndex);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (myProxy->rowCount(newIndex) == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index.isValid())
|
2015-07-12 20:48:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
index = QModelIndex();
|
|
|
|
break;
|
2015-07-12 20:48:54 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
index = newIndex;
|
2015-07-12 20:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myProxy->sort(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::clear()
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myModel->clear();
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::setEditable(bool editable)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myModel->setEditable(editable);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool FileTreeView::edit(QModelIndex const& index, EditTrigger trigger, QEvent* event)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (selectionModel()->selectedRows().size() != 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const nameIndex = index.sibling(index.row(), FileTreeModel::COL_NAME);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (editTriggers().testFlag(trigger))
|
|
|
|
{
|
|
|
|
selectionModel()->setCurrentIndex(nameIndex, QItemSelectionModel::NoUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QTreeView::edit(nameIndex, trigger, event);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::checkSelectedItems()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myModel->setWanted(selectedSourceRows(), true);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::uncheckSelectedItems()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myModel->setWanted(selectedSourceRows(), false);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::onlyCheckSelectedItems()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const rootIndex = myModel->index(0, 0);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!rootIndex.isValid())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndexList wantedIndices = selectedSourceRows();
|
|
|
|
myModel->setWanted(wantedIndices, true);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
qSort(wantedIndices);
|
|
|
|
|
|
|
|
QSet<QModelIndex> wantedIndicesParents;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : wantedIndices)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (QModelIndex p = i.parent(); p.isValid(); p = p.parent())
|
|
|
|
{
|
|
|
|
wantedIndicesParents.insert(p);
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QQueue<QModelIndex> parentsQueue;
|
|
|
|
parentsQueue.enqueue(rootIndex);
|
|
|
|
QModelIndexList unwantedIndices;
|
|
|
|
|
|
|
|
while (!parentsQueue.isEmpty())
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const parentIndex = parentsQueue.dequeue();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (qBinaryFind(wantedIndices, parentIndex) != wantedIndices.end())
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0, count = myModel->rowCount(parentIndex); i < count; ++i)
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const childIndex = parentIndex.child(i, 0);
|
|
|
|
int const childCheckState = childIndex.data(FileTreeModel::WantedRole).toInt();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (childCheckState == Qt::Unchecked || qBinaryFind(wantedIndices, childIndex) != wantedIndices.end())
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
continue;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (childCheckState == Qt::Checked && childIndex.data(FileTreeModel::FileIndexRole).toInt() >= 0)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
|
|
|
unwantedIndices << childIndex;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!wantedIndicesParents.contains(childIndex))
|
|
|
|
{
|
|
|
|
unwantedIndices << childIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parentsQueue.enqueue(childIndex);
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myModel->setWanted(unwantedIndices, false);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::setSelectedItemsPriority()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QAction* action = qobject_cast<QAction*>(sender());
|
|
|
|
assert(action != nullptr);
|
|
|
|
myModel->setPriority(selectedSourceRows(), action->property(PRIORITY_KEY).toInt());
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool FileTreeView::openSelectedItem()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myModel->openFile(myProxy->mapToSource(currentIndex()));
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::renameSelectedItem()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::edit(currentIndex());
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::refreshContextMenuActionsSensitivity()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(myContextMenu != nullptr);
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndexList const selectedRows = selectionModel()->selectedRows();
|
|
|
|
Qt::CheckState const checkState = getCumulativeCheckState(selectedRows);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const haveSelection = !selectedRows.isEmpty();
|
|
|
|
bool const haveSingleSelection = selectedRows.size() == 1;
|
|
|
|
bool const haveUnchecked = checkState == Qt::Unchecked || checkState == Qt::PartiallyChecked;
|
|
|
|
bool const haveChecked = checkState == Qt::Checked || checkState == Qt::PartiallyChecked;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
myCheckSelectedAction->setEnabled(haveUnchecked);
|
|
|
|
myUncheckSelectedAction->setEnabled(haveChecked);
|
|
|
|
myOnlyCheckSelectedAction->setEnabled(haveSelection);
|
|
|
|
myPriorityMenu->setEnabled(haveSelection);
|
|
|
|
myOpenAction->setEnabled(haveSingleSelection && selectedRows.first().data(FileTreeModel::FileIndexRole).toInt() >= 0 &&
|
|
|
|
selectedRows.first().data(FileTreeModel::CompleteRole).toBool());
|
|
|
|
myRenameAction->setEnabled(haveSingleSelection);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::initContextMenu()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myContextMenu = new QMenu(this);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myCheckSelectedAction = myContextMenu->addAction(tr("Check Selected"), this, SLOT(checkSelectedItems()));
|
|
|
|
myUncheckSelectedAction = myContextMenu->addAction(tr("Uncheck Selected"), this, SLOT(uncheckSelectedItems()));
|
|
|
|
myOnlyCheckSelectedAction = myContextMenu->addAction(tr("Only Check Selected"), this, SLOT(onlyCheckSelectedItems()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myContextMenu->addSeparator();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myPriorityMenu = myContextMenu->addMenu(tr("Priority"));
|
|
|
|
myHighPriorityAction = myPriorityMenu->addAction(FileTreeItem::tr("High"), this, SLOT(setSelectedItemsPriority()));
|
|
|
|
myNormalPriorityAction = myPriorityMenu->addAction(FileTreeItem::tr("Normal"), this, SLOT(setSelectedItemsPriority()));
|
|
|
|
myLowPriorityAction = myPriorityMenu->addAction(FileTreeItem::tr("Low"), this, SLOT(setSelectedItemsPriority()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myHighPriorityAction->setProperty(PRIORITY_KEY, TR_PRI_HIGH);
|
|
|
|
myNormalPriorityAction->setProperty(PRIORITY_KEY, TR_PRI_NORMAL);
|
|
|
|
myLowPriorityAction->setProperty(PRIORITY_KEY, TR_PRI_LOW);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myContextMenu->addSeparator();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myOpenAction = myContextMenu->addAction(tr("Open"), this, SLOT(openSelectedItem()));
|
|
|
|
myRenameAction = myContextMenu->addAction(tr("Rename..."), this, SLOT(renameSelectedItem()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(myContextMenu, SIGNAL(aboutToShow()), SLOT(refreshContextMenuActionsSensitivity()));
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndexList FileTreeView::selectedSourceRows(int column) const
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndexList indices;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : selectionModel()->selectedRows(column))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
indices << myProxy->mapToSource(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
2015-08-16 22:07:09 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
Qt::CheckState FileTreeView::getCumulativeCheckState(QModelIndexList const& indices)
|
2015-08-16 22:07:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool haveChecked = false, haveUnchecked = false;
|
2015-08-16 22:07:09 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : indices)
|
2015-08-16 22:07:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (i.data(FileTreeModel::WantedRole).toInt())
|
2015-08-16 22:07:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case Qt::Checked:
|
2015-08-16 22:07:09 +00:00
|
|
|
haveChecked = true;
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case Qt::Unchecked:
|
2015-08-16 22:07:09 +00:00
|
|
|
haveUnchecked = true;
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case Qt::PartiallyChecked:
|
2015-08-16 22:07:09 +00:00
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (haveChecked && haveUnchecked)
|
|
|
|
{
|
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
}
|
2015-08-16 22:07:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return haveChecked ? Qt::Checked : Qt::Unchecked;
|
2015-08-16 22:07:09 +00:00
|
|
|
}
|