Skip to content

Commit

Permalink
Prohibit editing documents in the recycle bin
Browse files Browse the repository at this point in the history
  • Loading branch information
iljukhaput committed Nov 7, 2024
1 parent 8d82755 commit 3e9bbdc
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 85 deletions.
204 changes: 120 additions & 84 deletions src/core/management_layer/content/project/project_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class ProjectManager::Implementation
/**
* @brief Открыть текущий документ в отдельном окне
*/
void openCurrentDocumentInNewWindow();
void openCurrentDocumentInNewWindow(bool _readOnly);

/**
* @brief Режим редакитрования заданного документа
Expand Down Expand Up @@ -263,10 +263,30 @@ class ProjectManager::Implementation
void emptyRecycleBin();

/**
* @brief Отсортировать по алфавиту дочерние элеиенты
* @brief Находится ли итем в корзине
*/
bool isInRecycleBin(BusinessLayer::StructureModelItem* _item) const;

/**
* @brief Отсортировать по алфавиту дочерние элементы
*/
void sortChildrenAlphabetically();

/**
* @brief Получить интерфейс активного редактора документа
*/
Ui::IDocumentView* activeDocumentView() const;

/**
* @brief Получить интерфейс неактивного редактора документа
*/
Ui::IDocumentView* inactiveDocumentView() const;

/**
* @brief Обновить режим редактирования представлений
*/
void updateViewsEditingMode();

//
// Данные
//
Expand Down Expand Up @@ -552,14 +572,14 @@ void ProjectManager::Implementation::updateNavigatorContextMenu(const QModelInde
else {
auto addFolder = new QAction(tr("Add folder"));
addFolder->setIconText(u8"\U000F0257");
addFolder->setEnabled(enabled);
addFolder->setEnabled(enabled && !isInRecycleBin(currentItem));
connect(addFolder, &QAction::triggered, q,
[this] { addDocument(Domain::DocumentObjectType::Folder); });
menuActions.append(addFolder);
//
auto addDocument = new QAction(tr("Add document"));
addDocument->setIconText(u8"\U000F0415");
addDocument->setEnabled(enabled);
addDocument->setEnabled(enabled && !isInRecycleBin(currentItem));
connect(addDocument, &QAction::triggered, q, [this] { this->addDocument(); });
menuActions.append(addDocument);

Expand Down Expand Up @@ -624,8 +644,9 @@ void ProjectManager::Implementation::updateNavigatorContextMenu(const QModelInde
auto openInNewWindow = new QAction(tr("Open in new window"));
openInNewWindow->setSeparator(!isDocumentActionAdded && !menuActions.isEmpty());
openInNewWindow->setIconText(u8"\U000F03CC");
connect(openInNewWindow, &QAction::triggered, q,
[this] { openCurrentDocumentInNewWindow(); });
connect(openInNewWindow, &QAction::triggered, q, [this, currentItem] {
openCurrentDocumentInNewWindow(isInRecycleBin(currentItem));
});
menuActions.append(openInNewWindow);
isDocumentActionAdded = true;
}
Expand Down Expand Up @@ -694,7 +715,7 @@ void ProjectManager::Implementation::updateNavigatorContextMenu(const QModelInde
auto shareAccess = new QAction(tr("Share access"));
shareAccess->setSeparator(!menuActions.isEmpty());
shareAccess->setIconText(u8"\U000F0010");
connect(shareAccess, &QAction::triggered, q, [this, documentIndexToShare] {
connect(shareAccess, &QAction::triggered, q, [this, currentItem, documentIndexToShare] {
auto projectCollaboratorsView = pluginsBuilder.projectCollaboratorsView(
modelsFacade.modelFor(Domain::DocumentObjectType::Project));
view.left->addEditor(projectCollaboratorsView->asQWidget());
Expand Down Expand Up @@ -737,7 +758,7 @@ void ProjectManager::Implementation::setCurrentItem(BusinessLayer::StructureMode
setCurrentIndex(itemIndex);
}

void ProjectManager::Implementation::openCurrentDocumentInNewWindow()
void ProjectManager::Implementation::openCurrentDocumentInNewWindow(bool _readOnly)
{
if (view.activeModel == nullptr) {
return;
Expand All @@ -764,6 +785,10 @@ void ProjectManager::Implementation::openCurrentDocumentInNewWindow()
DocumentEditingMode ProjectManager::Implementation::documentEditingMode(
BusinessLayer::StructureModelItem* _documentItem) const
{
if (_documentItem->isReadOnly() || isInRecycleBin(_documentItem)) {
return DocumentEditingMode::Read;
}

if (editingPermissions.isEmpty()) {
return editingMode;
}
Expand Down Expand Up @@ -1696,6 +1721,20 @@ void ProjectManager::Implementation::emptyRecycleBin()
QObject::connect(dialog, &Dialog::disappeared, dialog, &Dialog::deleteLater);
}

bool ProjectManager::Implementation::isInRecycleBin(BusinessLayer::StructureModelItem* _item) const
{
if (_item == nullptr) {
return false;
}

for (auto parent = _item->parent(); parent != nullptr; parent = parent->parent()) {
if (parent->type() == Domain::DocumentObjectType::RecycleBin) {
return true;
}
}
return false;
}

void ProjectManager::Implementation::sortChildrenAlphabetically()
{
const auto currentItemIndex
Expand All @@ -1710,6 +1749,46 @@ void ProjectManager::Implementation::sortChildrenAlphabetically()
projectStructureModel->saveChanges();
}

Ui::IDocumentView* ProjectManager::Implementation::activeDocumentView() const
{
Ui::IDocumentView* documentView = nullptr;
auto plugin = pluginsBuilder.plugin(view.activeViewMimeType);
if (plugin != nullptr) {
if (view.active == view.left) {
documentView = plugin->view(view.activeModel);
} else {
documentView = plugin->secondaryView(view.activeModel);
}
}
return documentView;
}

Ui::IDocumentView* ProjectManager::Implementation::inactiveDocumentView() const
{
Ui::IDocumentView* documentView = nullptr;
auto plugin = pluginsBuilder.plugin(view.inactiveViewMimeType);
if (plugin != nullptr) {
if (view.inactive == view.left) {
documentView = plugin->view(view.inactiveModel);
} else {
documentView = plugin->secondaryView(view.inactiveModel);
}
}
return documentView;
}

void ProjectManager::Implementation::updateViewsEditingMode()
{
if (auto activeView = activeDocumentView(); activeView != nullptr) {
const auto item = projectStructureModel->itemForIndex(view.activeIndex);
activeView->setEditingMode(documentEditingMode(item));
}
if (auto inactiveView = inactiveDocumentView(); inactiveView != nullptr) {
const auto item = projectStructureModel->itemForIndex(view.inactiveIndex);
inactiveView->setEditingMode(documentEditingMode(item));
}
}


// ****

Expand Down Expand Up @@ -2054,6 +2133,25 @@ ProjectManager::ProjectManager(QObject* _parent, QWidget* _parentWidget,
}
}
});

connect(d->projectStructureModel, &BusinessLayer::StructureModel::rowsMoved, this,
[this](const QModelIndex& _sourceParent, int _sourceStart, int _sourceEnd,
const QModelIndex& _destinationParent) {
const auto sourceParentItem = d->projectStructureModel->itemForIndex(_sourceParent);
const auto destinationItem
= d->projectStructureModel->itemForIndex(_destinationParent);
if (sourceParentItem == nullptr || destinationItem == nullptr) {
return;
}
//
// Обновляем режим редактирования представлений при перемещении в корзину или из неё
//
if (destinationItem->type() == Domain::DocumentObjectType::RecycleBin
|| sourceParentItem->type() == Domain::DocumentObjectType::RecycleBin) {
d->updateViewsEditingMode();
}
});

connect(
d->projectStructureModel, &BusinessLayer::StructureModel::rowsAboutToBeMoved, this,
[this](const QModelIndex& _sourceParent, int _sourceStart, int _sourceEnd,
Expand Down Expand Up @@ -4099,134 +4197,71 @@ void ProjectManager::setAvailableCredits(int _credits)

void ProjectManager::setRephrasedText(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setRephrasedText(_text);
}
}

void ProjectManager::setExpandedText(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setExpandedText(_text);
}
}

void ProjectManager::setShortenedText(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setShortenedText(_text);
}
}

void ProjectManager::setInsertedText(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setInsertedText(_text);
}
}

void ProjectManager::setSummarizeedText(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setSummarizedText(_text);
}
}

void ProjectManager::setTranslatedText(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setTranslatedText(_text);
}
}

void ProjectManager::setGeneratedSynopsis(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setGeneratedSynopsis(_text);
}
}

void ProjectManager::setGeneratedText(const QString& _text)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setGeneratedText(_text);
}
}

void ProjectManager::setGeneratedImage(const QPixmap& _image)
{
auto plugin = d->pluginsBuilder.plugin(d->view.activeViewMimeType);
Ui::IDocumentView* view = nullptr;
if (d->view.active == d->view.left) {
view = plugin->view(d->view.activeModel);
} else {
view = plugin->secondaryView(d->view.activeModel);
}

auto view = d->activeDocumentView();
if (view != nullptr) {
view->setGeneratedImage(_image);
}
Expand Down Expand Up @@ -4463,15 +4498,15 @@ void ProjectManager::showView(const QModelIndex& _itemIndex, const QString& _vie
}
Log::trace("Set project info");
view->setProjectInfo(d->isProjectRemote, d->isProjectOwner, d->allowGrantAccessToProject);
Log::trace("Set editing mode");
view->setEditingMode(d->documentEditingMode(itemForShow));
Log::trace("Set view cursors");
view->setCursors({ d->collaboratorsCursors.begin(), d->collaboratorsCursors.end() });
Log::trace("Set document versions");
d->view.active->setDocumentDraft(aliasedItem->versions());
Log::trace("Show view");
d->view.active->showEditor(view->asQWidget());
d->view.activeIndex = sourceItemIndex;
Log::trace("Set editing mode");
d->updateViewsEditingMode();

//
// Связываем редакторы
Expand Down Expand Up @@ -4655,8 +4690,7 @@ void ProjectManager::showViewForVersion(BusinessLayer::StructureModelItem* _item
d->view.active->showNotImplementedPage();
return;
}
view->setEditingMode(_item->isReadOnly() ? DocumentEditingMode::Read
: d->documentEditingMode(_item));
d->updateViewsEditingMode();
view->setCursors({ d->collaboratorsCursors.begin(), d->collaboratorsCursors.end() });
d->view.active->showEditor(view->asQWidget());

Expand Down Expand Up @@ -4710,6 +4744,7 @@ void ProjectManager::activateView(const QModelIndex& _itemIndex, const QString&
if (view == nullptr) {
return;
}
d->updateViewsEditingMode();

//
// Настроим опции редактора
Expand Down Expand Up @@ -4757,6 +4792,7 @@ void ProjectManager::showNavigator(const QModelIndex& _itemIndex, const QString&
d->navigator->showProjectNavigator();
return;
}
d->updateViewsEditingMode();

//
// Связываем редактор с навигатоом
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class CORE_LIBRARY_EXPORT StructureModel : public AbstractModel
StructureModelItem* itemForType(Domain::DocumentObjectType _type) const;

/**
* @brief Получить элемент корзины
* @brief Переместить элемент в корзину
*/
void moveItemToRecycleBin(StructureModelItem* _item);

Expand Down

0 comments on commit 3e9bbdc

Please sign in to comment.