/Beeftext/Combo/ComboEditor.cpp

https://github.com/xmichelo/Beeftext · C++ · 146 lines · 93 code · 24 blank · 29 comment · 1 complexity · bf18969d1d55d4eeafba9379add7b440 MD5 · raw file

  1. /// \file
  2. /// \author Xavier Michelon
  3. ///
  4. /// \brief Implementation of combo editor widget.
  5. ///
  6. /// Copyright (c) Xavier Michelon. All rights reserved.
  7. /// Licensed under the MIT License. See LICENSE file in the project root for full license information.
  8. #include "stdafx.h"
  9. #include "ComboEditor.h"
  10. #include "BeeftextConstants.h"
  11. //**********************************************************************************************************************
  12. /// \param[in] parent The parent widget of the editor.
  13. //**********************************************************************************************************************
  14. ComboEditor::ComboEditor(QWidget* parent)
  15. : QWidget(parent)
  16. {
  17. ui_.setupUi(this);
  18. connect(ui_.snippetEdit, &QPlainTextEdit::customContextMenuRequested, this,
  19. &ComboEditor::onEditorContextMenuRequested);
  20. }
  21. //**********************************************************************************************************************
  22. /// \return The snippet edit widget
  23. //**********************************************************************************************************************
  24. QPlainTextEdit* ComboEditor::plainTextEdit() const
  25. {
  26. return ui_.snippetEdit;
  27. }
  28. //**********************************************************************************************************************
  29. /// \return The content of the edit
  30. //**********************************************************************************************************************
  31. QString ComboEditor::plainText() const
  32. {
  33. return ui_.snippetEdit->toPlainText();
  34. }
  35. //**********************************************************************************************************************
  36. //
  37. //**********************************************************************************************************************
  38. void ComboEditor::setPlainText(QString const& text) const
  39. {
  40. ui_.snippetEdit->setPlainText(text);
  41. }
  42. //**********************************************************************************************************************
  43. /// \return The menu.
  44. //**********************************************************************************************************************
  45. QMenu* ComboEditor::createComboVariableMenu()
  46. {
  47. QMenu* menu = new QMenu(tr("&Insert Variable"), this);
  48. QAction* action = new QAction(tr("Clip&board Content"), this);
  49. connect(action, &QAction::triggered, [&]() { this->insertTextInSnippetEdit("#{clipboard}", false); });
  50. menu->addAction(action);
  51. QMenu* dtMenu = new QMenu(tr("&Date/Time"));
  52. action = new QAction(tr("D&ate"), this);
  53. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{date}", false); });
  54. dtMenu->addAction(action);
  55. action = new QAction(tr("&Time"), this);
  56. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{time}", false); });
  57. dtMenu->addAction(action);
  58. action = new QAction(tr("Dat&e && Time"), this);
  59. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{dateTime}", false); });
  60. dtMenu->addAction(action);
  61. action = new QAction(tr("&Custom Date && Time"), this);
  62. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{dateTime:}", true); });
  63. dtMenu->addAction(action);
  64. menu->addMenu(dtMenu);
  65. action = new QAction(tr("C&ursor Position"), this);
  66. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{cursor}", false); });
  67. menu->addAction(action);
  68. QMenu* comboMenu = new QMenu(tr("Co&mbo"), this);
  69. action = new QAction(tr("Co&mbo"), this);
  70. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{combo:}", true); });
  71. comboMenu->addAction(action);
  72. action = new QAction(tr("Combo (&uppercase)"), this);
  73. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{upper:}", true); });
  74. comboMenu->addAction(action);
  75. action = new QAction(tr("Combo (&lowercase)"), this);
  76. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{lower:}", true); });
  77. comboMenu->addAction(action);
  78. menu->addMenu(comboMenu);
  79. action = new QAction(tr("Combo (&trimmed)"), this);
  80. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{trim:}", true); });
  81. comboMenu->addAction(action);
  82. menu->addMenu(comboMenu);
  83. action = new QAction(tr("En&vironment Variable"), this);
  84. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{envVar:}", true); });
  85. menu->addAction(action);
  86. action = new QAction(tr("User &Input"), this);
  87. connect(action, &QAction::triggered, [this]() { this->insertTextInSnippetEdit("#{input:}", true); });
  88. menu->addAction(action);
  89. menu->addSeparator();
  90. action = new QAction(tr("&About Variables"), this);
  91. connect(action, &QAction::triggered, []() { QDesktopServices::openUrl(constants::kBeeftextWikiVariablesUrl); });
  92. menu->addAction(action);
  93. return menu;
  94. }
  95. //**********************************************************************************************************************
  96. /// \param[in] text The text to insert
  97. /// \param[in] move1CharLeft Should the cursor be moved by one character to the left after insertion
  98. //**********************************************************************************************************************
  99. void ComboEditor::insertTextInSnippetEdit(QString const& text, bool move1CharLeft) const
  100. {
  101. QTextCursor cursor = ui_.snippetEdit->textCursor();
  102. cursor.beginEditBlock();
  103. cursor.insertText(text);
  104. if (move1CharLeft)
  105. {
  106. cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1);
  107. cursor.endEditBlock();
  108. ui_.snippetEdit->setTextCursor(cursor); ///< Required for the cursor position change to take effect
  109. }
  110. else
  111. cursor.endEditBlock();
  112. }
  113. //**********************************************************************************************************************
  114. /// \param[in] pos The cursos position.
  115. //**********************************************************************************************************************
  116. void ComboEditor::onEditorContextMenuRequested(QPoint const& pos)
  117. {
  118. QScopedPointer<QMenu, QScopedPointerDeleteLater> menu(ui_.snippetEdit->createStandardContextMenu(pos));
  119. menu->addSeparator();
  120. menu->addMenu(this->createComboVariableMenu());
  121. menu->exec(ui_.snippetEdit->viewport()->mapToGlobal(pos));
  122. }