|
| 1 | +/* |
| 2 | + * This file is part of Notepad Next. |
| 3 | + * Copyright 2026 NotepadAI contributors |
| 4 | + * |
| 5 | + * Notepad Next is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Notepad Next is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with Notepad Next. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "InteractiveRebaseDialog.h" |
| 20 | + |
| 21 | +#include <QColor> |
| 22 | +#include <QFile> |
| 23 | +#include <QHBoxLayout> |
| 24 | +#include <QHeaderView> |
| 25 | +#include <QPushButton> |
| 26 | +#include <QTableView> |
| 27 | +#include <QTextStream> |
| 28 | +#include <QVBoxLayout> |
| 29 | + |
| 30 | +// --- InteractiveRebaseModel --- |
| 31 | + |
| 32 | +InteractiveRebaseModel::InteractiveRebaseModel(QObject *parent) |
| 33 | + : QAbstractTableModel(parent) |
| 34 | +{ |
| 35 | +} |
| 36 | + |
| 37 | +void InteractiveRebaseModel::setEntries(const QVector<RebaseEntry> &entries) |
| 38 | +{ |
| 39 | + beginResetModel(); |
| 40 | + m_entries = entries; |
| 41 | + endResetModel(); |
| 42 | +} |
| 43 | + |
| 44 | +int InteractiveRebaseModel::rowCount(const QModelIndex &) const |
| 45 | +{ |
| 46 | + return m_entries.size(); |
| 47 | +} |
| 48 | + |
| 49 | +int InteractiveRebaseModel::columnCount(const QModelIndex &) const |
| 50 | +{ |
| 51 | + return 4; |
| 52 | +} |
| 53 | + |
| 54 | +QVariant InteractiveRebaseModel::data(const QModelIndex &index, int role) const |
| 55 | +{ |
| 56 | + if (!index.isValid() || index.row() >= m_entries.size()) return {}; |
| 57 | + |
| 58 | + const auto &e = m_entries[index.row()]; |
| 59 | + |
| 60 | + if (role == Qt::DisplayRole || role == Qt::EditRole) { |
| 61 | + switch (index.column()) { |
| 62 | + case 0: return actionToString(e.action); |
| 63 | + case 1: return e.hash.left(7); |
| 64 | + case 2: return e.subject; |
| 65 | + case 3: return e.author; |
| 66 | + default: break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (role == Qt::ForegroundRole && e.action == RebaseEntry::Drop) { |
| 71 | + return QColor(Qt::gray); |
| 72 | + } |
| 73 | + |
| 74 | + return {}; |
| 75 | +} |
| 76 | + |
| 77 | +QVariant InteractiveRebaseModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 78 | +{ |
| 79 | + if (orientation != Qt::Horizontal || role != Qt::DisplayRole) return {}; |
| 80 | + switch (section) { |
| 81 | + case 0: return tr("Action"); |
| 82 | + case 1: return tr("SHA"); |
| 83 | + case 2: return tr("Message"); |
| 84 | + case 3: return tr("Author"); |
| 85 | + default: break; |
| 86 | + } |
| 87 | + return {}; |
| 88 | +} |
| 89 | + |
| 90 | +Qt::ItemFlags InteractiveRebaseModel::flags(const QModelIndex &index) const |
| 91 | +{ |
| 92 | + Qt::ItemFlags f = QAbstractTableModel::flags(index); |
| 93 | + if (index.column() == 0) |
| 94 | + f |= Qt::ItemIsEditable; |
| 95 | + f |= Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; |
| 96 | + return f; |
| 97 | +} |
| 98 | + |
| 99 | +bool InteractiveRebaseModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 100 | +{ |
| 101 | + if (!index.isValid() || role != Qt::EditRole || index.column() != 0) return false; |
| 102 | + m_entries[index.row()].action = stringToAction(value.toString()); |
| 103 | + emit dataChanged(index, index, {Qt::DisplayRole, Qt::ForegroundRole}); |
| 104 | + return true; |
| 105 | +} |
| 106 | + |
| 107 | +bool InteractiveRebaseModel::moveRows(const QModelIndex &, int sourceRow, int count, |
| 108 | + const QModelIndex &, int destinationChild) |
| 109 | +{ |
| 110 | + if (count != 1) return false; |
| 111 | + if (sourceRow == destinationChild || sourceRow < 0 || sourceRow >= m_entries.size()) return false; |
| 112 | + |
| 113 | + int dest = destinationChild > sourceRow ? destinationChild + 1 : destinationChild; |
| 114 | + beginMoveRows({}, sourceRow, sourceRow, {}, dest); |
| 115 | + m_entries.move(sourceRow, destinationChild); |
| 116 | + endMoveRows(); |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +QString InteractiveRebaseModel::actionToString(RebaseEntry::Action a) |
| 121 | +{ |
| 122 | + switch (a) { |
| 123 | + case RebaseEntry::Pick: return QStringLiteral("pick"); |
| 124 | + case RebaseEntry::Reword: return QStringLiteral("reword"); |
| 125 | + case RebaseEntry::Edit: return QStringLiteral("edit"); |
| 126 | + case RebaseEntry::Squash: return QStringLiteral("squash"); |
| 127 | + case RebaseEntry::Fixup: return QStringLiteral("fixup"); |
| 128 | + case RebaseEntry::Drop: return QStringLiteral("drop"); |
| 129 | + } |
| 130 | + return QStringLiteral("pick"); |
| 131 | +} |
| 132 | + |
| 133 | +RebaseEntry::Action InteractiveRebaseModel::stringToAction(const QString &s) |
| 134 | +{ |
| 135 | + if (s == QLatin1String("pick") || s == QLatin1String("p")) return RebaseEntry::Pick; |
| 136 | + if (s == QLatin1String("reword") || s == QLatin1String("r")) return RebaseEntry::Reword; |
| 137 | + if (s == QLatin1String("edit") || s == QLatin1String("e")) return RebaseEntry::Edit; |
| 138 | + if (s == QLatin1String("squash") || s == QLatin1String("s")) return RebaseEntry::Squash; |
| 139 | + if (s == QLatin1String("fixup") || s == QLatin1String("f")) return RebaseEntry::Fixup; |
| 140 | + if (s == QLatin1String("drop") || s == QLatin1String("d")) return RebaseEntry::Drop; |
| 141 | + return RebaseEntry::Pick; |
| 142 | +} |
| 143 | + |
| 144 | +// --- InteractiveRebaseDialog --- |
| 145 | + |
| 146 | +InteractiveRebaseDialog::InteractiveRebaseDialog(const QString &todoFilePath, QWidget *parent) |
| 147 | + : QDialog(parent) |
| 148 | + , m_todoPath(todoFilePath) |
| 149 | +{ |
| 150 | + setWindowTitle(tr("Interactive Rebase")); |
| 151 | + setMinimumSize(700, 400); |
| 152 | + resize(800, 500); |
| 153 | + |
| 154 | + auto *layout = new QVBoxLayout(this); |
| 155 | + |
| 156 | + m_model = new InteractiveRebaseModel(this); |
| 157 | + m_table = new QTableView(this); |
| 158 | + m_table->setModel(m_model); |
| 159 | + m_table->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 160 | + m_table->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 161 | + m_table->setDragEnabled(true); |
| 162 | + m_table->setAcceptDrops(true); |
| 163 | + m_table->setDragDropMode(QAbstractItemView::InternalMove); |
| 164 | + m_table->setDropIndicatorShown(true); |
| 165 | + m_table->verticalHeader()->setVisible(false); |
| 166 | + m_table->horizontalHeader()->setStretchLastSection(true); |
| 167 | + m_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
| 168 | + m_table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
| 169 | + m_table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch); |
| 170 | + m_table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents); |
| 171 | + layout->addWidget(m_table, 1); |
| 172 | + |
| 173 | + auto *btnRow = new QHBoxLayout; |
| 174 | + m_moveUpBtn = new QPushButton(tr("Move Up"), this); |
| 175 | + m_moveDownBtn = new QPushButton(tr("Move Down"), this); |
| 176 | + btnRow->addWidget(m_moveUpBtn); |
| 177 | + btnRow->addWidget(m_moveDownBtn); |
| 178 | + btnRow->addStretch(); |
| 179 | + m_cancelBtn = new QPushButton(tr("Cancel"), this); |
| 180 | + m_startBtn = new QPushButton(tr("Start Rebase"), this); |
| 181 | + m_startBtn->setDefault(true); |
| 182 | + btnRow->addWidget(m_cancelBtn); |
| 183 | + btnRow->addWidget(m_startBtn); |
| 184 | + layout->addLayout(btnRow); |
| 185 | + |
| 186 | + connect(m_moveUpBtn, &QPushButton::clicked, this, &InteractiveRebaseDialog::onMoveUp); |
| 187 | + connect(m_moveDownBtn, &QPushButton::clicked, this, &InteractiveRebaseDialog::onMoveDown); |
| 188 | + connect(m_startBtn, &QPushButton::clicked, this, &InteractiveRebaseDialog::onStartRebase); |
| 189 | + connect(m_cancelBtn, &QPushButton::clicked, this, &QDialog::reject); |
| 190 | + |
| 191 | + parseTodoFile(todoFilePath); |
| 192 | +} |
| 193 | + |
| 194 | +void InteractiveRebaseDialog::onMoveUp() |
| 195 | +{ |
| 196 | + auto rows = m_table->selectionModel()->selectedRows(); |
| 197 | + if (rows.isEmpty()) return; |
| 198 | + int row = rows.first().row(); |
| 199 | + if (row <= 0) return; |
| 200 | + m_model->moveRows({}, row, 1, {}, row - 1); |
| 201 | + m_table->selectRow(row - 1); |
| 202 | +} |
| 203 | + |
| 204 | +void InteractiveRebaseDialog::onMoveDown() |
| 205 | +{ |
| 206 | + auto rows = m_table->selectionModel()->selectedRows(); |
| 207 | + if (rows.isEmpty()) return; |
| 208 | + int row = rows.first().row(); |
| 209 | + if (row >= m_model->rowCount() - 1) return; |
| 210 | + m_model->moveRows({}, row, 1, {}, row + 1); |
| 211 | + m_table->selectRow(row + 1); |
| 212 | +} |
| 213 | + |
| 214 | +void InteractiveRebaseDialog::onStartRebase() |
| 215 | +{ |
| 216 | + writeTodoFile(m_todoPath); |
| 217 | + m_accepted = true; |
| 218 | + accept(); |
| 219 | +} |
| 220 | + |
| 221 | +void InteractiveRebaseDialog::parseTodoFile(const QString &path) |
| 222 | +{ |
| 223 | + QFile f(path); |
| 224 | + if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return; |
| 225 | + |
| 226 | + QVector<RebaseEntry> entries; |
| 227 | + QTextStream stream(&f); |
| 228 | + while (!stream.atEnd()) { |
| 229 | + QString line = stream.readLine().trimmed(); |
| 230 | + if (line.isEmpty() || line.startsWith(QLatin1Char('#'))) continue; |
| 231 | + if (line.startsWith(QLatin1String("noop"))) continue; |
| 232 | + |
| 233 | + // Format: <action> <hash> <subject> |
| 234 | + int firstSpace = line.indexOf(QLatin1Char(' ')); |
| 235 | + if (firstSpace < 0) continue; |
| 236 | + int secondSpace = line.indexOf(QLatin1Char(' '), firstSpace + 1); |
| 237 | + if (secondSpace < 0) secondSpace = line.size(); |
| 238 | + |
| 239 | + RebaseEntry entry; |
| 240 | + entry.action = InteractiveRebaseModel::stringToAction(line.left(firstSpace)); |
| 241 | + entry.hash = line.mid(firstSpace + 1, secondSpace - firstSpace - 1); |
| 242 | + entry.subject = line.mid(secondSpace + 1); |
| 243 | + entries.append(entry); |
| 244 | + } |
| 245 | + |
| 246 | + m_model->setEntries(entries); |
| 247 | +} |
| 248 | + |
| 249 | +void InteractiveRebaseDialog::writeTodoFile(const QString &path) |
| 250 | +{ |
| 251 | + QFile f(path); |
| 252 | + if (!f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) return; |
| 253 | + |
| 254 | + QTextStream stream(&f); |
| 255 | + const auto entries = m_model->entries(); |
| 256 | + for (const auto &e : entries) { |
| 257 | + stream << InteractiveRebaseModel::actionToString(e.action) |
| 258 | + << ' ' << e.hash << ' ' << e.subject << '\n'; |
| 259 | + } |
| 260 | +} |
0 commit comments