| 1 /* |
|
| 2 * LDForge: LDraw parts authoring CAD |
|
| 3 * Copyright (C) 2013 Santeri Piippo |
|
| 4 * |
|
| 5 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. |
|
| 17 */ |
|
| 18 |
|
| 19 #include <qboxlayout.h> |
|
| 20 #include <qmessagebox.h> |
|
| 21 #include <QGridLayout> |
|
| 22 #include "historyDialog.h" |
|
| 23 #include "history.h" |
|
| 24 #include "colors.h" |
|
| 25 #include "file.h" |
|
| 26 |
|
| 27 EXTERN_ACTION (undo); |
|
| 28 EXTERN_ACTION (redo); |
|
| 29 |
|
| 30 // ============================================================================= |
|
| 31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
| 32 // ============================================================================= |
|
| 33 HistoryDialog::HistoryDialog (QWidget* parent, Qt::WindowFlags f) : QDialog (parent, f) { |
|
| 34 historyList = new QListWidget; |
|
| 35 undoButton = new QPushButton ("Undo"); |
|
| 36 redoButton = new QPushButton ("Redo"); |
|
| 37 clearButton = new QPushButton ("Clear"); |
|
| 38 buttons = new QDialogButtonBox (QDialogButtonBox::Close); |
|
| 39 |
|
| 40 historyList->setAlternatingRowColors (true); |
|
| 41 |
|
| 42 undoButton->setIcon (getIcon ("undo")); |
|
| 43 redoButton->setIcon (getIcon ("redo")); |
|
| 44 |
|
| 45 connect (undoButton, SIGNAL (clicked ()), this, SLOT (slot_undo ())); |
|
| 46 connect (redoButton, SIGNAL (clicked ()), this, SLOT (slot_redo ())); |
|
| 47 connect (clearButton, SIGNAL (clicked ()), this, SLOT (slot_clear ())); |
|
| 48 connect (buttons, SIGNAL (rejected ()), this, SLOT (reject ())); |
|
| 49 connect (historyList, SIGNAL (itemSelectionChanged ()), this, SLOT (slot_selChanged ())); |
|
| 50 |
|
| 51 QVBoxLayout* qButtonLayout = new QVBoxLayout; |
|
| 52 qButtonLayout->setDirection (QBoxLayout::TopToBottom); |
|
| 53 qButtonLayout->addWidget (undoButton); |
|
| 54 qButtonLayout->addWidget (redoButton); |
|
| 55 qButtonLayout->addWidget (clearButton); |
|
| 56 qButtonLayout->addStretch (); |
|
| 57 |
|
| 58 QGridLayout* qLayout = new QGridLayout; |
|
| 59 qLayout->setColumnStretch (0, 1); |
|
| 60 qLayout->addWidget (historyList, 0, 0, 2, 1); |
|
| 61 qLayout->addLayout (qButtonLayout, 0, 1); |
|
| 62 qLayout->addWidget (buttons, 1, 1); |
|
| 63 |
|
| 64 setLayout (qLayout); |
|
| 65 setWindowIcon (getIcon ("history")); |
|
| 66 setWindowTitle ("Edit History"); |
|
| 67 |
|
| 68 populateList (); |
|
| 69 updateButtons (); |
|
| 70 updateSelection (); |
|
| 71 } |
|
| 72 |
|
| 73 // ============================================================================= |
|
| 74 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
| 75 // ============================================================================= |
|
| 76 void HistoryDialog::populateList () { |
|
| 77 historyList->clear (); |
|
| 78 |
|
| 79 QListWidgetItem* qItem = new QListWidgetItem; |
|
| 80 qItem->setText ("[[ initial state ]]"); |
|
| 81 qItem->setIcon (getIcon ("empty")); |
|
| 82 historyList->addItem (qItem); |
|
| 83 |
|
| 84 #if 0 |
|
| 85 for (AbstractHistoryEntry* entry : g_curfile->history ().entries ()) { |
|
| 86 str text; |
|
| 87 QIcon entryIcon; |
|
| 88 |
|
| 89 switch (entry->type ()) { |
|
| 90 default: |
|
| 91 text = "???"; |
|
| 92 break; |
|
| 93 } |
|
| 94 |
|
| 95 QListWidgetItem* item = new QListWidgetItem; |
|
| 96 item->setText (text); |
|
| 97 item->setIcon (entryIcon); |
|
| 98 historyList->addItem (item); |
|
| 99 } |
|
| 100 #endif |
|
| 101 } |
|
| 102 |
|
| 103 // ============================================================================= |
|
| 104 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
| 105 // ============================================================================= |
|
| 106 void HistoryDialog::slot_undo () { |
|
| 107 g_curfile->undo (); |
|
| 108 updateButtons (); |
|
| 109 updateSelection (); |
|
| 110 } |
|
| 111 |
|
| 112 // ============================================================================= |
|
| 113 void HistoryDialog::slot_redo () { |
|
| 114 g_curfile->redo (); |
|
| 115 updateButtons (); |
|
| 116 updateSelection (); |
|
| 117 } |
|
| 118 |
|
| 119 void HistoryDialog::updateSelection () { |
|
| 120 historyList->setCurrentItem (historyList->item (g_curfile->history ().pos () + 1)); |
|
| 121 } |
|
| 122 |
|
| 123 // ============================================================================= |
|
| 124 void HistoryDialog::slot_clear () { |
|
| 125 if (!confirm ("Are you sure you want to clear the edit history?\nThis cannot be undone.")) |
|
| 126 return; // Canceled |
|
| 127 |
|
| 128 g_curfile->clearHistory (); |
|
| 129 populateList (); |
|
| 130 updateButtons (); |
|
| 131 } |
|
| 132 |
|
| 133 // ============================================================================= |
|
| 134 void HistoryDialog::updateButtons () { |
|
| 135 undoButton->setEnabled (ACTION (undo)->isEnabled ()); |
|
| 136 redoButton->setEnabled (ACTION (redo)->isEnabled ()); |
|
| 137 } |
|
| 138 |
|
| 139 // ============================================================================= |
|
| 140 void HistoryDialog::slot_selChanged () { |
|
| 141 if (historyList->selectedItems ().size () != 1) |
|
| 142 return; |
|
| 143 |
|
| 144 QListWidgetItem* item = historyList->selectedItems ()[0]; |
|
| 145 |
|
| 146 // Find the index of the edit |
|
| 147 long index; |
|
| 148 for (index = 0; index < historyList->count (); ++index) |
|
| 149 if (historyList->item (index) == item) |
|
| 150 break; |
|
| 151 |
|
| 152 // qHistoryList is 0-based, History is -1-based, thus decrement |
|
| 153 // the index now |
|
| 154 index--; |
|
| 155 |
|
| 156 // Seek to the selected edit by repeadetly undoing or redoing. |
|
| 157 while (g_curfile->history ().pos () != index) { |
|
| 158 if (g_curfile->history ().pos () > index) |
|
| 159 g_curfile->undo (); |
|
| 160 else |
|
| 161 g_curfile->redo (); |
|
| 162 } |
|
| 163 |
|
| 164 updateButtons (); |
|
| 165 } |
|