|
| 1 | +#include "extcmdlinewidget.h" |
| 2 | + |
| 3 | +#include <QDebug> |
| 4 | +#include <QCoreApplication> |
| 5 | + |
| 6 | +namespace NeovimQt { namespace Cmdline { |
| 7 | + |
| 8 | +ExtCmdlineWidget::ExtCmdlineWidget(ShellWidget* parent) |
| 9 | +{ |
| 10 | + setParent(parent); |
| 11 | + setVisible(false); |
| 12 | + |
| 13 | + m_cmdTextBox = new QRichLineEdit(); |
| 14 | + m_cmdBlockText = new BlockDisplay(); |
| 15 | + |
| 16 | + m_vLayout = new QVBoxLayout(); |
| 17 | + m_vLayout->addWidget(m_cmdBlockText); |
| 18 | + m_vLayout->addWidget(m_cmdTextBox); |
| 19 | + setLayout(m_vLayout); |
| 20 | + |
| 21 | + // The size of this widget must change when text content changes. |
| 22 | + connect(m_cmdTextBox, SIGNAL(textChanged(QString)), this, |
| 23 | + SLOT(resizeLineEditToContent())); |
| 24 | +} |
| 25 | + |
| 26 | +void ExtCmdlineWidget::handleCmdlineShow( |
| 27 | + const QVariantList& content, |
| 28 | + int pos, |
| 29 | + const QString& firstc, |
| 30 | + const QString& prompt, |
| 31 | + int indent, |
| 32 | + int level) |
| 33 | +{ |
| 34 | + QString cmdlineContent; |
| 35 | + |
| 36 | + LineModel lineModel{ content, pos, firstc, prompt, indent, level }; |
| 37 | + if (level > m_model.size()) { |
| 38 | + m_model.append(std::move(lineModel)); |
| 39 | + } |
| 40 | + else { |
| 41 | + m_model[level - 1] = std::move(lineModel); |
| 42 | + } |
| 43 | + |
| 44 | + m_cmdTextBox->setText(m_model.last().getPromptText()); |
| 45 | + |
| 46 | + updateGeometry(); |
| 47 | + show(); |
| 48 | + |
| 49 | + setCursorPosition(pos); |
| 50 | +} |
| 51 | + |
| 52 | +void ExtCmdlineWidget::handleCmdlinePos( |
| 53 | + int pos, |
| 54 | + int level) |
| 55 | +{ |
| 56 | + if (level > m_model.size()) { |
| 57 | + qDebug() << "Invalid value for 'level'" << level; |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + m_model[level - 1].m_position = pos; |
| 62 | + setCursorPosition(pos); |
| 63 | +} |
| 64 | + |
| 65 | +void ExtCmdlineWidget::handleCmdlineSpecialChar( |
| 66 | + const QString& c, |
| 67 | + bool shift, |
| 68 | + int level) |
| 69 | +{ |
| 70 | + if (level > m_model.size()) { |
| 71 | + qDebug() << "Invalid value for 'level'" << level; |
| 72 | + } |
| 73 | + |
| 74 | + LineModel& line = m_model[level - 1]; |
| 75 | + |
| 76 | + // Special characters are inserted in two steps |
| 77 | + if (shift) { |
| 78 | + // Step One: insert a placeholder character. |
| 79 | + line.m_content.insert(line.m_position, c); |
| 80 | + } |
| 81 | + else { |
| 82 | + // Step Two: replace the placeholder with the desired character. |
| 83 | + line.m_content.replace(line.m_position, 1, c); |
| 84 | + } |
| 85 | + |
| 86 | + m_cmdTextBox->setText(line.getPromptText()); |
| 87 | +} |
| 88 | + |
| 89 | +void ExtCmdlineWidget::handleCmdlineHide() |
| 90 | +{ |
| 91 | + if (!m_model.empty()) { |
| 92 | + m_model.pop_back(); |
| 93 | + } |
| 94 | + |
| 95 | + hide(); |
| 96 | +} |
| 97 | + |
| 98 | +void ExtCmdlineWidget::handleCmdlineBlockShow(const QVariantList& lines) |
| 99 | +{ |
| 100 | + QString blockText; |
| 101 | + for (const auto& varLine: lines) { |
| 102 | + QVariantList line = varLine.toList(); |
| 103 | + qDebug() << line; |
| 104 | + for (const auto& varSegment : line) { |
| 105 | + QVariantList segment = varSegment.toList(); |
| 106 | + const QString segmentText = segment.at(1).toString(); |
| 107 | + |
| 108 | + blockText += segmentText; |
| 109 | + } |
| 110 | + } |
| 111 | + m_cmdBlockText->setText(blockText); |
| 112 | + m_cmdBlockText->updateGeometry(); |
| 113 | + m_cmdBlockText->show(); |
| 114 | +} |
| 115 | + |
| 116 | +void ExtCmdlineWidget::handleCmdlineBlockAppend(const QVariantList& line) |
| 117 | +{ |
| 118 | + for (const auto& varSegment : line) { |
| 119 | + QVariantList segment = varSegment.toList(); |
| 120 | + const QString segmentText = segment.at(1).toString(); |
| 121 | + |
| 122 | + m_cmdBlockText->append(segmentText); |
| 123 | + } |
| 124 | + |
| 125 | + m_cmdBlockText->updateGeometry(); |
| 126 | +} |
| 127 | + |
| 128 | +void ExtCmdlineWidget::handleCmdlineBlockHide() |
| 129 | +{ |
| 130 | + m_cmdBlockText->hide(); |
| 131 | +} |
| 132 | + |
| 133 | +void ExtCmdlineWidget::resizeLineEditToContent() |
| 134 | +{ |
| 135 | + updateGeometry(); |
| 136 | +} |
| 137 | + |
| 138 | +void ExtCmdlineWidget::updateGeometry() |
| 139 | +{ |
| 140 | + if (!parentWidget()) { |
| 141 | + qDebug() << "No parentWidget, cannot update size/position"; |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + ShellWidget* parentShellWidget{ static_cast<ShellWidget*>(parentWidget()) }; |
| 146 | + |
| 147 | + const int maxWidth = parentShellWidget->width() * 0.75; |
| 148 | + const int maxHeight = parentShellWidget->height() * 0.75; |
| 149 | + |
| 150 | + const QSize sizeHintThis = sizeHint(); |
| 151 | + const int width = qMin(sizeHintThis.width(), maxWidth); |
| 152 | + const int height = qMin(sizeHintThis.height(), maxHeight); |
| 153 | + |
| 154 | + const int anchorX = (parentShellWidget->width() - width) / 2; |
| 155 | + const int anchorY = (parentShellWidget->height() - height) / 2; |
| 156 | + |
| 157 | + setGeometry(anchorX, anchorY, width, height); |
| 158 | + setMaximumWidth(maxWidth); |
| 159 | + setMaximumHeight(maxHeight); |
| 160 | + QWidget::updateGeometry(); |
| 161 | +} |
| 162 | + |
| 163 | +void ExtCmdlineWidget::setCursorPosition(int pos) |
| 164 | +{ |
| 165 | + if (!parentWidget()) { |
| 166 | + qDebug() << "No parentWidget, cannot update cursor position"; |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + ShellWidget* parentShellWidget{ static_cast<ShellWidget*>(parentWidget()) }; |
| 171 | + |
| 172 | + // Neovim cursor position is set by inverting the foreground/background colors, Qt cursor hidden. |
| 173 | + const int posCursorReal = pos + m_model.back().m_prompt.size() + m_model.back().m_indent + 1; |
| 174 | + QTextCharFormat fmtInverse; |
| 175 | + fmtInverse.setBackground(parentShellWidget->foreground()); |
| 176 | + fmtInverse.setForeground(parentShellWidget->background()); |
| 177 | + m_cmdTextBox->setTextFormat({ { posCursorReal, 1, fmtInverse} }); |
| 178 | + |
| 179 | + // Ensures ':' is shown when users scrolls full left |
| 180 | + if (pos == 0) { |
| 181 | + m_cmdTextBox->setCursorPosition(0); |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + // Ensure the entire nvim cursor is visble by setting the Qt cursor position. |
| 186 | + // Moving left: left edge of cursor. Moving right: right edge of cursor. |
| 187 | + static int last_pos = 0; |
| 188 | + |
| 189 | + int cursorPositionQt = posCursorReal; |
| 190 | + if (last_pos <= pos) { |
| 191 | + ++cursorPositionQt; |
| 192 | + } |
| 193 | + m_cmdTextBox->setCursorPosition(cursorPositionQt); |
| 194 | + |
| 195 | + last_pos = pos; |
| 196 | +} |
| 197 | + |
| 198 | +int ExtCmdlineWidget::getMaxPromptLength() const |
| 199 | +{ |
| 200 | + int maxLineLength = 0; |
| 201 | + for (const auto& line : m_model) { |
| 202 | + maxLineLength = qMax(line.getPromptText().size(), maxLineLength); |
| 203 | + } |
| 204 | + |
| 205 | + return qMax(m_cmdBlockText->GetMaxLineLength(), maxLineLength); |
| 206 | +} |
| 207 | + |
| 208 | +QSize ExtCmdlineWidget::sizeHint() const |
| 209 | +{ |
| 210 | + QSize sizeHint = QFrame::sizeHint(); |
| 211 | + |
| 212 | + QFontMetrics fm = fontMetrics(); |
| 213 | + |
| 214 | + const int maxLineLength = getMaxPromptLength(); |
| 215 | + |
| 216 | + const int widthHint = fm.averageCharWidth() * (maxLineLength + 2) + |
| 217 | + layout()->contentsMargins().left() + layout()->contentsMargins().right(); |
| 218 | + |
| 219 | + constexpr int minWidth = 300; |
| 220 | + |
| 221 | + sizeHint.setWidth(qMax(widthHint, minWidth)); |
| 222 | + |
| 223 | + return sizeHint; |
| 224 | +} |
| 225 | + |
| 226 | +void ExtCmdlineWidget::updatePalette() |
| 227 | +{ |
| 228 | + if (!parentWidget()) { |
| 229 | + qDebug() << "No parentWidget, cannot set palette"; |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + ShellWidget* parentShellWidget{ static_cast<ShellWidget*>(parentWidget()) }; |
| 234 | + |
| 235 | + m_palette.setColor(QPalette::Base, parentShellWidget->background()); |
| 236 | + m_palette.setColor(QPalette::Text, parentShellWidget->foreground()); |
| 237 | + m_palette.setColor(QPalette::Background, parentShellWidget->foreground()); |
| 238 | + |
| 239 | + m_cmdTextBox->setPalette(m_palette); |
| 240 | + m_cmdBlockText->setPalette(m_palette); |
| 241 | +} |
| 242 | + |
| 243 | +void ExtCmdlineWidget::setFont(const QFont &font) |
| 244 | +{ |
| 245 | + m_cmdBlockText->setFont(font); |
| 246 | + m_cmdTextBox->setFont(font); |
| 247 | +} |
| 248 | + |
| 249 | +} } // namespace NeovimQt::Cmdline |
0 commit comments