diff --git a/sources/titleblock/qettemplateeditor.cpp b/sources/titleblock/qettemplateeditor.cpp index 09a2e1b63..78481ffc2 100644 --- a/sources/titleblock/qettemplateeditor.cpp +++ b/sources/titleblock/qettemplateeditor.cpp @@ -499,6 +499,7 @@ void QETTitleBlockTemplateEditor::selectedCellsChanged(QList s } else { template_cell_editor_widget_ -> setVisible(false); } + updateActions(); } /** @@ -561,10 +562,15 @@ void QETTitleBlockTemplateEditor::updateEditorTitle() { adequate actions. */ void QETTitleBlockTemplateEditor::updateActions() { - /// TODO complete this method save_ -> setEnabled(!read_only_); - merge_cells_ -> setEnabled(!read_only_); - split_cell_ -> setEnabled(!read_only_); + + bool can_merge; + bool can_split; + if (!read_only_) { + template_edition_area_view_ -> analyzeSelectedCells(&can_merge, &can_split); + } + merge_cells_ -> setEnabled(!read_only_ && can_merge); + split_cell_ -> setEnabled(!read_only_ && can_split); } /** diff --git a/sources/titleblock/templatecommands.cpp b/sources/titleblock/templatecommands.cpp index df91b299b..d05f37f8a 100644 --- a/sources/titleblock/templatecommands.cpp +++ b/sources/titleblock/templatecommands.cpp @@ -577,8 +577,7 @@ MergeCellsCommand::MergeCellsCommand(const TitleBlockTemplateCellsSet &merged_ce row_span_after_(-1), col_span_after_(-1) { - // basic check - if (merged_cells.count() < 2) return; + if (!canMerge(merged_cells, tbtemplate)) return; // the spanning cell is the top left cell TitleBlockTemplateVisualCell *top_left_cell = merged_cells.topLeftCell(); @@ -617,6 +616,25 @@ MergeCellsCommand::MergeCellsCommand(const TitleBlockTemplateCellsSet &merged_ce MergeCellsCommand::~MergeCellsCommand() { } +/** + @param merged_cells Cell sto be merged. + @param tbtemplate Modified title block template. + @return true if the merge is feasible, false otherwise +*/ +bool MergeCellsCommand::canMerge(const TitleBlockTemplateCellsSet &merged_cells, TitleBlockTemplate *tbtemplate) { + // basic checks + if (!merged_cells.isRectangle()) return(false); + if (merged_cells.count() < 2) return(false); + + // the spanning cell is the top left cell + TitleBlockTemplateVisualCell *top_left_cell = merged_cells.topLeftCell(); + if (!top_left_cell || !top_left_cell -> cell()) return(false); + + if (!getBottomRightCell(merged_cells)) return(false); + + return(true); +} + /** @return true if this command object is valid and usable, false otherwise. */ @@ -703,17 +721,10 @@ SplitCellsCommand::SplitCellsCommand(const TitleBlockTemplateCellsSet &splitted_ row_span_before_(-1), col_span_before_(-1) { - // basic check: the command applies to a single visual cell only - if (splitted_cells.count() != 1) return; - - // fetch the spanning cell - spanning_cell_ = splitted_cells.first() -> cell(); - if (!spanning_cell_) return; - - // ensure the cell spans over other cells and therefore can be splitted - if (!spanning_cell_ -> spans()) return; + if (!canSplit(splitted_cells, tbtemplate)) return; // retrieve values necessary for the undo operation + spanning_cell_ = splitted_cells.first() -> cell(); spanned_cells_ = tbtemplate_ -> spannedCells(spanning_cell_); row_span_before_ = spanning_cell_ -> row_span; col_span_before_ = spanning_cell_ -> col_span; @@ -734,6 +745,27 @@ SplitCellsCommand::SplitCellsCommand(const TitleBlockTemplateCellsSet &splitted_ SplitCellsCommand::~SplitCellsCommand() { } +/** + @param splitted_cells Cell to be splitted. + @param tbtemplate Modified title block template. + @return true if the split is feasible, false otherwise +*/ +bool SplitCellsCommand::canSplit(const TitleBlockTemplateCellsSet &splitted_cells, TitleBlockTemplate *tbtemplate) { + Q_UNUSED(tbtemplate) + + // basic check: the command applies to a single visual cell only + if (splitted_cells.count() != 1) return(false); + + // fetch the spanning cell + TitleBlockCell *spanning_cell = splitted_cells.first() -> cell(); + if (!spanning_cell) return(false); + + // ensure the cell spans over other cells and therefore can be splitted + if (!spanning_cell -> spans()) return(false); + + return(true); +} + /** @return true if this command object is valid and usable, false otherwise. */ diff --git a/sources/titleblock/templatecommands.h b/sources/titleblock/templatecommands.h index 10dbcb650..5debf1394 100644 --- a/sources/titleblock/templatecommands.h +++ b/sources/titleblock/templatecommands.h @@ -180,6 +180,7 @@ class MergeCellsCommand : public TitleBlockTemplateCommand { // methods public: + static bool canMerge(const TitleBlockTemplateCellsSet &, TitleBlockTemplate *); bool isValid() const; virtual void undo(); virtual void redo(); @@ -210,6 +211,7 @@ class SplitCellsCommand : public TitleBlockTemplateCommand { // methods public: + static bool canSplit(const TitleBlockTemplateCellsSet &splitted_cells, TitleBlockTemplate *tbtemplate); bool isValid() const; virtual void undo(); virtual void redo(); diff --git a/sources/titleblock/templateview.cpp b/sources/titleblock/templateview.cpp index 8f580bf6e..de66ed127 100644 --- a/sources/titleblock/templateview.cpp +++ b/sources/titleblock/templateview.cpp @@ -251,19 +251,6 @@ void TitleBlockTemplateView::mergeSelectedCells() { // retrieve the selected cells TitleBlockTemplateCellsSet selected_cells = selectedCellsSet(); - // merging applies only to cells composing a rectangle - if (!selected_cells.isRectangle()) { - qDebug() << "selected cells are not composing a rectangle"; - return; - } - - // the merge area may also be too small - if (selected_cells.count() < 2) { - qDebug() << "the merge area does not even contain 2 selected and mergeable cells"; - return; - } - - qDebug() << Q_FUNC_INFO << "ok, ready for cells merge"; MergeCellsCommand *merge_command = new MergeCellsCommand(selected_cells, tbtemplate_); if (merge_command -> isValid()) requestGridModification(merge_command); } @@ -275,12 +262,6 @@ void TitleBlockTemplateView::splitSelectedCell() { // retrieve the selected cells TitleBlockTemplateCellsSet selected_cells = selectedCellsSet(); - // we expect only one visual cell to be selected - if (selected_cells.count() != 1) { - qDebug() << "please select a single cell"; - return; - } - SplitCellsCommand *split_command = new SplitCellsCommand(selected_cells, tbtemplate_); if (split_command -> isValid()) requestGridModification(split_command); } @@ -319,6 +300,30 @@ TitleBlockTemplateCellsSet TitleBlockTemplateView::cells(const QRectF &rect) con return(makeCellsSetFromGraphicsItems(items)); } +/** + @param can_merge If non-zero, will be changed to reflect whether selected cells may be merged + @param can_merge If non-zero, will be changed to reflect whether selected cells may be splitted +*/ +void TitleBlockTemplateView::analyzeSelectedCells(bool *can_merge, bool *can_split) { + if (!can_merge && !can_split) return; + + if (!tbtemplate_) { + if (can_merge) *can_merge = false; + if (can_split) *can_split = false; + return; + } + + // retrieve the selected cells + TitleBlockTemplateCellsSet selected_cells = selectedCellsSet(); + + if (can_merge) { + *can_merge = MergeCellsCommand::canMerge(selected_cells, tbtemplate_); + } + if (can_split) { + *can_split = SplitCellsCommand::canSplit(selected_cells, tbtemplate_); + } +} + /** @return the current size of the rendered title block template */ diff --git a/sources/titleblock/templateview.h b/sources/titleblock/templateview.h index cf091ab54..e8825f915 100644 --- a/sources/titleblock/templateview.h +++ b/sources/titleblock/templateview.h @@ -46,6 +46,7 @@ class TitleBlockTemplateView : public QGraphicsView { virtual QList selectedCells() const; virtual TitleBlockTemplateCellsSet selectedCellsSet() const; virtual TitleBlockTemplateCellsSet cells(const QRectF &) const; + virtual void analyzeSelectedCells(bool *, bool *); virtual QSizeF templateSize() const; virtual qreal templateWidth() const; virtual qreal templateHeight() const;