#include "QtPathListBox.h" #include #include #include #include #include "QtPathListBoxItem.h" #include "utilityFile.h" QtPathListBox::QtPathListBox(QWidget *parent, const QString& listName, SelectionPolicyType selectionPolicy) : QtListBox(parent, listName) , m_selectionPolicy(selectionPolicy) { QLabel* dropInfoText = new QLabel("Drop Files & Folders"); dropInfoText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); dropInfoText->setObjectName("dropInfo"); dropInfoText->setAlignment(Qt::AlignRight); addWidgetToBar(dropInfoText); } QtPathListBox::SelectionPolicyType QtPathListBox::getSelectionPolicy() const { return m_selectionPolicy; } const FilePath& QtPathListBox::getRelativeRootDirectory() const { return m_relativeRootDirectory; } void QtPathListBox::setRelativeRootDirectory(const FilePath& dir) { m_relativeRootDirectory = dir; } std::vector QtPathListBox::getPathsAsDisplayed() const { std::vector paths; for (int i = 0; i < m_list->count(); ++i) { QtListBoxItem* widget = dynamic_cast(m_list->itemWidget(m_list->item(i))); paths.push_back(FilePath(widget->getText().toStdWString())); } return paths; } std::vector QtPathListBox::getPathsAsAbsolute() const { std::vector paths = getPathsAsDisplayed(); for (FilePath& path: paths) { makeAbsolute(path); } return paths; } void QtPathListBox::setPaths(const std::vector& list, bool readOnly) { clear(); for (FilePath path : list) { QtListBoxItem* item = addListBoxItemWithText(QString::fromStdWString(path.wstr())); item->setReadOnly(readOnly); } } void QtPathListBox::makeAbsolute(FilePath& path) const { if (!path.empty() && !path.isAbsolute() && !m_relativeRootDirectory.empty()) { path = m_relativeRootDirectory.getConcatenated(path); } } void QtPathListBox::makeRelativeIfShorter(FilePath& path) const { path = utility::getAsRelativeIfShorter(path, m_relativeRootDirectory); } void QtPathListBox::dropEvent(QDropEvent *event) { foreach(QUrl url, event->mimeData()->urls()) { FilePath path(url.toLocalFile().toStdWString()); makeRelativeIfShorter(path); addListBoxItemWithText(QString::fromStdWString(path.wstr())); } } void QtPathListBox::dragEnterEvent(QDragEnterEvent *event) { event->accept(); } QtListBoxItem* QtPathListBox::createListBoxItem(QListWidgetItem* item) { return new QtPathListBoxItem(this, item); }