1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
//////////////////////////////////////////////////////////////////////////////
// breezeexceptionlistwidget.cpp
// -------------------
//
// SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
//
// SPDX-License-Identifier: MIT
//////////////////////////////////////////////////////////////////////////////
#include "breezeexceptionlistwidget.h"
#include "breezeexceptiondialog.h"
#include <KLocalizedString>
#include <QIcon>
#include <QMessageBox>
#include <QPointer>
#include <QRegularExpression>
//__________________________________________________________
namespace Breeze
{
//__________________________________________________________
ExceptionListWidget::ExceptionListWidget(QWidget *parent)
: QWidget(parent)
{
// ui
m_ui.setupUi(this);
// list
m_ui.exceptionListView->setAllColumnsShowFocus(true);
m_ui.exceptionListView->setRootIsDecorated(false);
m_ui.exceptionListView->setSortingEnabled(false);
m_ui.exceptionListView->setModel(&model());
m_ui.exceptionListView->sortByColumn(ExceptionModel::ColumnType, Qt::AscendingOrder);
m_ui.exceptionListView->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
m_ui.moveUpButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-up")));
m_ui.moveDownButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down")));
m_ui.addButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
m_ui.removeButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
m_ui.editButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
connect(m_ui.addButton, &QAbstractButton::clicked, this, &ExceptionListWidget::add);
connect(m_ui.editButton, &QAbstractButton::clicked, this, &ExceptionListWidget::edit);
connect(m_ui.removeButton, &QAbstractButton::clicked, this, &ExceptionListWidget::remove);
connect(m_ui.moveUpButton, &QAbstractButton::clicked, this, &ExceptionListWidget::up);
connect(m_ui.moveDownButton, &QAbstractButton::clicked, this, &ExceptionListWidget::down);
connect(m_ui.exceptionListView, &QAbstractItemView::activated, this, &ExceptionListWidget::edit);
connect(m_ui.exceptionListView, &QAbstractItemView::clicked, this, &ExceptionListWidget::toggle);
connect(m_ui.exceptionListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ExceptionListWidget::updateButtons);
updateButtons();
resizeColumns();
}
//__________________________________________________________
void ExceptionListWidget::setExceptions(const InternalSettingsList &exceptions)
{
model().set(exceptions);
resizeColumns();
setChanged(false);
}
//__________________________________________________________
InternalSettingsList ExceptionListWidget::exceptions()
{
return model().get();
setChanged(false);
}
//__________________________________________________________
void ExceptionListWidget::updateButtons()
{
bool hasSelection(!m_ui.exceptionListView->selectionModel()->selectedRows().empty());
m_ui.removeButton->setEnabled(hasSelection);
m_ui.editButton->setEnabled(hasSelection);
m_ui.moveUpButton->setEnabled(hasSelection && !m_ui.exceptionListView->selectionModel()->isRowSelected(0, QModelIndex()));
m_ui.moveDownButton->setEnabled(hasSelection && !m_ui.exceptionListView->selectionModel()->isRowSelected(model().rowCount() - 1, QModelIndex()));
}
//_______________________________________________________
void ExceptionListWidget::add()
{
QPointer<ExceptionDialog> dialog = new ExceptionDialog(this);
dialog->setWindowTitle(i18n("New Exception - Breeze Settings"));
InternalSettingsPtr exception(new InternalSettings());
exception->load();
dialog->setException(exception);
// run dialog and check existence
if (!dialog->exec()) {
delete dialog;
return;
}
dialog->save();
delete dialog;
// check exceptions
if (!checkException(exception)) {
return;
}
// create new item
model().add(exception);
setChanged(true);
// make sure item is selected
QModelIndex index(model().index(exception));
if (index != m_ui.exceptionListView->selectionModel()->currentIndex()) {
m_ui.exceptionListView->selectionModel()->select(index, QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Rows);
m_ui.exceptionListView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Current | QItemSelectionModel::Rows);
}
resizeColumns();
}
//_______________________________________________________
void ExceptionListWidget::edit()
{
// retrieve selection
QModelIndex current(m_ui.exceptionListView->selectionModel()->currentIndex());
if (!model().contains(current)) {
return;
}
InternalSettingsPtr exception(model().get(current));
// create dialog
QPointer<ExceptionDialog> dialog(new ExceptionDialog(this));
dialog->setWindowTitle(i18n("Edit Exception - Breeze Settings"));
dialog->setException(exception);
// map dialog
if (!dialog->exec()) {
delete dialog;
return;
}
// check modifications
if (!dialog->isChanged()) {
return;
}
// retrieve exception
dialog->save();
delete dialog;
// check new exception validity
checkException(exception);
resizeColumns();
setChanged(true);
}
//_______________________________________________________
void ExceptionListWidget::remove()
{
// confirmation dialog
{
QMessageBox messageBox(QMessageBox::Question,
i18n("Question - Breeze Settings"),
i18n("Remove selected exception?"),
QMessageBox::Yes | QMessageBox::Cancel);
messageBox.button(QMessageBox::Yes)->setText(i18n("Remove"));
messageBox.setDefaultButton(QMessageBox::Cancel);
if (messageBox.exec() == QMessageBox::Cancel) {
return;
}
}
// remove
model().remove(model().get(m_ui.exceptionListView->selectionModel()->selectedRows()));
resizeColumns();
updateButtons();
setChanged(true);
}
//_______________________________________________________
void ExceptionListWidget::toggle(const QModelIndex &index)
{
if (!model().contains(index)) {
return;
}
if (index.column() != ExceptionModel::ColumnEnabled) {
return;
}
// get matching exception
InternalSettingsPtr exception(model().get(index));
exception->setEnabled(!exception->enabled());
setChanged(true);
}
//_______________________________________________________
void ExceptionListWidget::up()
{
InternalSettingsList selection(model().get(m_ui.exceptionListView->selectionModel()->selectedRows()));
if (selection.empty()) {
return;
}
// retrieve selected indexes in list and store in model
QModelIndexList selectedIndices(m_ui.exceptionListView->selectionModel()->selectedRows());
InternalSettingsList selectedExceptions(model().get(selectedIndices));
InternalSettingsList currentException(model().get());
InternalSettingsList newExceptions;
for (InternalSettingsList::const_iterator iter = currentException.constBegin(); iter != currentException.constEnd(); ++iter) {
// check if new list is not empty, current index is selected and last index is not.
// if yes, move.
if (!(newExceptions.empty() || selectedIndices.indexOf(model().index(*iter)) == -1
|| selectedIndices.indexOf(model().index(newExceptions.back())) != -1)) {
InternalSettingsPtr last(newExceptions.back());
newExceptions.removeLast();
newExceptions.append(*iter);
newExceptions.append(last);
} else {
newExceptions.append(*iter);
}
}
model().set(newExceptions);
// restore selection
m_ui.exceptionListView->selectionModel()->select(model().index(selectedExceptions.front()),
QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Rows);
for (InternalSettingsList::const_iterator iter = selectedExceptions.constBegin(); iter != selectedExceptions.constEnd(); ++iter) {
m_ui.exceptionListView->selectionModel()->select(model().index(*iter), QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
setChanged(true);
}
//_______________________________________________________
void ExceptionListWidget::down()
{
InternalSettingsList selection(model().get(m_ui.exceptionListView->selectionModel()->selectedRows()));
if (selection.empty()) {
return;
}
// retrieve selected indexes in list and store in model
QModelIndexList selectedIndices(m_ui.exceptionListView->selectionModel()->selectedIndexes());
InternalSettingsList selectedExceptions(model().get(selectedIndices));
InternalSettingsList currentExceptions(model().get());
InternalSettingsList newExceptions;
InternalSettingsListIterator iter(currentExceptions);
iter.toBack();
while (iter.hasPrevious()) {
InternalSettingsPtr current(iter.previous());
// check if new list is not empty, current index is selected and last index is not.
// if yes, move.
if (!(newExceptions.empty() || selectedIndices.indexOf(model().index(current)) == -1
|| selectedIndices.indexOf(model().index(newExceptions.front())) != -1)) {
InternalSettingsPtr first(newExceptions.front());
newExceptions.removeFirst();
newExceptions.prepend(current);
newExceptions.prepend(first);
} else {
newExceptions.prepend(current);
}
}
model().set(newExceptions);
// restore selection
m_ui.exceptionListView->selectionModel()->select(model().index(selectedExceptions.front()),
QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Rows);
for (InternalSettingsList::const_iterator iter = selectedExceptions.constBegin(); iter != selectedExceptions.constEnd(); ++iter) {
m_ui.exceptionListView->selectionModel()->select(model().index(*iter), QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
setChanged(true);
}
//_______________________________________________________
void ExceptionListWidget::resizeColumns() const
{
m_ui.exceptionListView->resizeColumnToContents(ExceptionModel::ColumnEnabled);
m_ui.exceptionListView->resizeColumnToContents(ExceptionModel::ColumnType);
m_ui.exceptionListView->resizeColumnToContents(ExceptionModel::ColumnRegExp);
}
//_______________________________________________________
bool ExceptionListWidget::checkException(InternalSettingsPtr exception)
{
while (exception->exceptionPattern().isEmpty() || !QRegularExpression(exception->exceptionPattern()).isValid()) {
QMessageBox::warning(this, i18n("Warning - Breeze Settings"), i18n("Regular Expression syntax is incorrect"));
QPointer<ExceptionDialog> dialog(new ExceptionDialog(this));
dialog->setException(exception);
if (dialog->exec() == QDialog::Rejected) {
delete dialog;
return false;
}
dialog->save();
delete dialog;
}
return true;
}
}
|