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"
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
FileTreeView::FileTreeView(QWidget* parent, bool is_editable) :
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView(parent),
|
2020-05-27 21:53:12 +00:00
|
|
|
model_(new FileTreeModel(this, is_editable)),
|
|
|
|
proxy_(new QSortFilterProxyModel(this)),
|
|
|
|
delegate_(new FileTreeDelegate(this))
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
proxy_->setSourceModel(model_);
|
|
|
|
proxy_->setSortRole(FileTreeModel::SortRole);
|
|
|
|
proxy_->setSortCaseSensitivity(Qt::CaseInsensitive);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
setModel(proxy_);
|
|
|
|
setItemDelegate(delegate_);
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
connect(model_, SIGNAL(priorityChanged(QSet<int>, int)), this, SIGNAL(priorityChanged(QSet<int>, int)));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
connect(model_, SIGNAL(wantedChanged(QSet<int>, bool)), this, SIGNAL(wantedChanged(QSet<int>, bool)));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
connect(model_, SIGNAL(pathEdited(QString, QString)), this, SIGNAL(pathEdited(QString, QString)));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
connect(model_, SIGNAL(openRequested(QString)), this, SIGNAL(openRequested(QString)));
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void FileTreeView::onClicked(QModelIndex const& proxy_index)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const model_index = proxy_->mapToSource(proxy_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (model_index.column() == FileTreeModel::COL_WANTED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->twiddleWanted(QModelIndexList() << model_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (model_index.column() == FileTreeModel::COL_PRIORITY)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->twiddlePriority(QModelIndexList() << model_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int min_width = 0;
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QStringList item_texts;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-05-29 17:40:07 +00:00
|
|
|
item_texts << QStringLiteral("999.9 ") + Formatter::unitStr(Formatter::MEM, static_cast<Formatter::Size>(s));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_PROGRESS:
|
2020-05-29 17:40:07 +00:00
|
|
|
item_texts << QStringLiteral(" 100% ");
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_WANTED:
|
2020-05-27 21:53:12 +00:00
|
|
|
min_width = 20;
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_PRIORITY:
|
2020-05-27 21:53:12 +00:00
|
|
|
item_texts << FileTreeItem::tr("Low") << FileTreeItem::tr("Normal") << FileTreeItem::tr("High") <<
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int item_width = 0;
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QString const& item_text : item_texts)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
item_width = std::max(item_width, Utils::measureViewItem(this, item_text));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QString const header_text = model_->headerData(column, Qt::Horizontal).toString();
|
|
|
|
int header_width = Utils::measureHeaderItem(this->header(), header_text);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int const width = std::max(min_width, std::max(item_width, header_width));
|
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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->twiddleWanted(selectedSourceRows());
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->twiddlePriority(selectedSourceRows());
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const root_index = model_->index(0, 0);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!root_index.isValid())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (context_menu_ == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
initContextMenu();
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
context_menu_->popup(event->globalPos());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void FileTreeView::update(FileList const& files, bool update_fields)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const model_was_empty = proxy_->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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->addFile(file.index, file.filename, file.wanted, file.priority, file.size, file.have, update_fields);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (model_was_empty)
|
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
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QModelIndex index = proxy_->index(0, 0); index.isValid();)
|
2015-07-12 20:48:54 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const old_index = index;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
expand(old_index);
|
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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (int i = 0, count = proxy_->rowCount(old_index); i < count; ++i)
|
2015-07-12 20:48:54 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const new_index = proxy_->index(i, 0, old_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (proxy_->rowCount(new_index) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
index = new_index;
|
2015-07-12 20:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
proxy_->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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->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
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
QModelIndex const name_index = 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))
|
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
selectionModel()->setCurrentIndex(name_index, QItemSelectionModel::NoUpdate);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
return QTreeView::edit(name_index, 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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const root_index = model_->index(0, 0);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!root_index.isValid())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndexList wanted_indices = selectedSourceRows();
|
|
|
|
model_->setWanted(wanted_indices, true);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
std::sort(wanted_indices.begin(), wanted_indices.end());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QSet<QModelIndex> wanted_indices_parents;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QModelIndex const& i : wanted_indices)
|
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())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
wanted_indices_parents.insert(p);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QQueue<QModelIndex> parents_queue;
|
|
|
|
parents_queue.enqueue(root_index);
|
|
|
|
QModelIndexList unwanted_indices;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
while (!parents_queue.isEmpty())
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const parent_index = parents_queue.dequeue();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (std::binary_search(wanted_indices.begin(), wanted_indices.end(), parent_index))
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
auto const* parent_model = parent_index.model();
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (int i = 0, count = model_->rowCount(parent_index); i < count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
QModelIndex const child_index = parent_model->index(i, 0, parent_index);
|
2020-05-27 21:53:12 +00:00
|
|
|
int const child_check_state = child_index.data(FileTreeModel::WantedRole).toInt();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (child_check_state == Qt::Unchecked ||
|
|
|
|
std::binary_search(wanted_indices.begin(), wanted_indices.end(), child_index))
|
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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (child_check_state == Qt::Checked && child_index.data(FileTreeModel::FileIndexRole).toInt() >= 0)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
unwanted_indices << child_index;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!wanted_indices_parents.contains(child_index))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
unwanted_indices << child_index;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
parents_queue.enqueue(child_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->setWanted(unwanted_indices, 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
|
|
|
{
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* action = qobject_cast<QAction*>(sender());
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(action != nullptr);
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return model_->openFile(proxy_->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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
assert(context_menu_ != nullptr);
|
|
|
|
|
|
|
|
QModelIndexList const selected_rows = selectionModel()->selectedRows();
|
|
|
|
Qt::CheckState const check_state = getCumulativeCheckState(selected_rows);
|
|
|
|
|
|
|
|
bool const have_selection = !selected_rows.isEmpty();
|
|
|
|
bool const have_single_selection = selected_rows.size() == 1;
|
|
|
|
bool const have_unchecked = check_state == Qt::Unchecked || check_state == Qt::PartiallyChecked;
|
|
|
|
bool const have_checked = check_state == Qt::Checked || check_state == Qt::PartiallyChecked;
|
|
|
|
|
|
|
|
check_selected_action_->setEnabled(have_unchecked);
|
|
|
|
uncheck_selected_action_->setEnabled(have_checked);
|
|
|
|
only_check_selected_action_->setEnabled(have_selection);
|
|
|
|
priority_menu_->setEnabled(have_selection);
|
|
|
|
open_action_->setEnabled(have_single_selection && selected_rows.first().data(FileTreeModel::FileIndexRole).toInt() >= 0 &&
|
|
|
|
selected_rows.first().data(FileTreeModel::CompleteRole).toBool());
|
|
|
|
rename_action_->setEnabled(have_single_selection);
|
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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
context_menu_ = new QMenu(this);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
check_selected_action_ = context_menu_->addAction(tr("Check Selected"), this, SLOT(checkSelectedItems()));
|
|
|
|
uncheck_selected_action_ = context_menu_->addAction(tr("Uncheck Selected"), this, SLOT(uncheckSelectedItems()));
|
|
|
|
only_check_selected_action_ = context_menu_->addAction(tr("Only Check Selected"), this, SLOT(onlyCheckSelectedItems()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
context_menu_->addSeparator();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
priority_menu_ = context_menu_->addMenu(tr("Priority"));
|
|
|
|
high_priority_action_ = priority_menu_->addAction(FileTreeItem::tr("High"), this, SLOT(setSelectedItemsPriority()));
|
|
|
|
normal_priority_action_ = priority_menu_->addAction(FileTreeItem::tr("Normal"), this, SLOT(setSelectedItemsPriority()));
|
|
|
|
low_priority_action_ = priority_menu_->addAction(FileTreeItem::tr("Low"), this, SLOT(setSelectedItemsPriority()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
high_priority_action_->setProperty(PRIORITY_KEY, TR_PRI_HIGH);
|
|
|
|
normal_priority_action_->setProperty(PRIORITY_KEY, TR_PRI_NORMAL);
|
|
|
|
low_priority_action_->setProperty(PRIORITY_KEY, TR_PRI_LOW);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
context_menu_->addSeparator();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
open_action_ = context_menu_->addAction(tr("Open"), this, SLOT(openSelectedItem()));
|
|
|
|
rename_action_ = context_menu_->addAction(tr("Rename..."), this, SLOT(renameSelectedItem()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
connect(context_menu_, 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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
indices << proxy_->mapToSource(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool have_checked = false;
|
|
|
|
bool have_unchecked = 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:
|
2020-05-27 21:53:12 +00:00
|
|
|
have_checked = true;
|
2015-08-16 22:07:09 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case Qt::Unchecked:
|
2020-05-27 21:53:12 +00:00
|
|
|
have_unchecked = true;
|
2015-08-16 22:07:09 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case Qt::PartiallyChecked:
|
2015-08-16 22:07:09 +00:00
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (have_checked && have_unchecked)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
}
|
2015-08-16 22:07:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return have_checked ? Qt::Checked : Qt::Unchecked;
|
2015-08-16 22:07:09 +00:00
|
|
|
}
|