diff --git a/CMakeLists.txt b/CMakeLists.txt index ab4effa..2a7de5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,8 @@ set(kbddisplay_SRCS kbddisplay.cpp keyboardview.cpp keydialog.cpp styledialog.cpp + style.cpp + stylemodel.cpp ) set(kbddisplay_UIS kbddisplay.ui keydialog.ui styledialog.ui) diff --git a/freestyle2.xml b/freestyle2.xml new file mode 100644 index 0000000..fa2a89a --- /dev/null +++ b/freestyle2.xml @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/keyboardview.cpp b/keyboardview.cpp new file mode 100644 index 0000000..3e0ae2c --- /dev/null +++ b/keyboardview.cpp @@ -0,0 +1,159 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "keyboardview.h" +#include "qgraphicskeyitem.h" +#include +#include +#include +#include +#include + +KeyboardView::KeyboardView(QWidget* parent) + : QGraphicsView(parent) +{ + keyDialog = new KeyDialog(); + stylechooser = keyDialog->getStyleChooser(); + connect(stylechooser, SIGNAL(itemChanged(QListWidgetItem*)), + this, SLOT(setStyle(QListWidgetItem*))); + connect(stylechooser, SIGNAL(itemClicked(QListWidgetItem*)), + this, SLOT(setStyle(QListWidgetItem*))); +} + +void KeyboardView::setStyle(QListWidgetItem* item) +{ + if (currentKey) + currentKey->setStyle(item->data(Qt::DisplayRole).toString(), currentIndex); +} + +void KeyboardView::setModel(KeyItemModel* model) +{ + if (this->model != nullptr) + QObject::disconnect(this->model, 0, stylechooser, 0); + this->model = model; + connect(model, SIGNAL(stylesChanged()), stylechooser, SLOT(updateStyles())); + stylechooser->updateStyles(); +} + +void KeyboardView::contextMenuEvent(QContextMenuEvent* event) +{ + QGraphicsKeyItem *keyitem = nullptr; + foreach(QGraphicsItem *item, items(event->pos())) { + if (item->type() != QGraphicsKeyItem::Type) + continue; + keyitem = (QGraphicsKeyItem*)item; + break; + } + if (keyitem == nullptr) + return; + currentKey = keyitem->getKey(); + currentIndex = keyitem->getPartIndex(mapToScene(event->pos())); + keyDialog->exec(currentKey); + + +} + +void KeyboardView::keyPressEvent(QKeyEvent* event) +{ + /*qDebug("Key( %d ), nativeScanCode( %d ), nativeVirtualKey( %d )", + event->key(), event->nativeScanCode(), event->nativeVirtualKey() + );*/ + currentKey = model->codeToKeyItemMap[event->nativeScanCode()]; +// qDebug() << currentKey->keyId << " : " << event->nativeScanCode(); + keyDialog->exec(currentKey); + + QGraphicsView::keyPressEvent(event); +} + +void KeyboardView::autoMap() +{ + QList codes = model->codeToKeyItemMap.keys(); + QString platform = QGuiApplication::platformName(); + if (platform == "xcb") { + QProcess xmodmap; + xmodmap.setProgram("xmodmap"); + QStringList args; + args.append("-pk"); + xmodmap.setArguments(args); + xmodmap.start(); + if (!xmodmap.waitForFinished()) { + QMessageBox::warning(this, "Meh!", + "xmodmap failed or is not installed"); + return; + } + QString outString = QString(xmodmap.readAllStandardOutput()); + QStringList output = outString.split('\n'); + for(int i = 0; i < output.length(); i++) { + bool ok; + QStringList line = output[i].trimmed().split( + QRegExp("\\s"), QString::SkipEmptyParts); + if (line.length() < 3) + continue; + int code = line[0].toInt(&ok); + if (!ok) + continue; + KeyItem * key = model->codeToKeyItemMap[code]; + if (key == nullptr) + continue; + int value = line[1].toInt(&ok, 16); + QString name = line[2].mid(1, line[2].length()-2); + + if (name.length() > 1 && value < 255) + name = QChar(value); + key->labelTop=name; + key->updateItems(); + } + } + else + QMessageBox::information(this, "Meh!", + "sorry, this function currently only works on X11"); +} + + +void KeyboardView::exportSVG(QString filename) +{ + QSvgGenerator generator; + generator.setFileName(filename); + + QRect rect = scene()->sceneRect().toRect(); + const double_t scale = 3.0; + rect.setRect(rect.x() * scale, rect.y() * scale, + rect.width() * scale, rect.height() * scale); + + + generator.setSize(rect.size()); + generator.setViewBox(rect); + + QPainter painter( &generator ); + scene()->render( &painter ); +} + + + + + + + + + + + + + + diff --git a/keyboardview.h b/keyboardview.h new file mode 100644 index 0000000..9c242fa --- /dev/null +++ b/keyboardview.h @@ -0,0 +1,51 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef KEYBOARDVIEW_H +#define KEYBOARDVIEW_H + +#include +#include "stylechooser.h" +#include "keyitemmodel.h" +#include "keydialog.h" + +class KeyboardView : public QGraphicsView +{ + Q_OBJECT +public: + KeyboardView(QWidget* parent = 0); + void setModel(KeyItemModel* model); + KeyDialog *keyDialog; +public slots: + void setStyle(QListWidgetItem* item); + void exportSVG(QString filename); + void autoMap(); + //void updateItem(KeyItem* key); + +protected: + KeyItem * currentKey = nullptr; + int currentIndex = 0; + virtual void contextMenuEvent(QContextMenuEvent* event); + StyleChooser *stylechooser; + KeyItemModel *model = nullptr; + virtual void keyPressEvent(QKeyEvent *event); + bool autoMapping = false; +}; + +#endif // KEYBOARDVIEW_H diff --git a/keydialog.cpp b/keydialog.cpp new file mode 100644 index 0000000..0a9a2b6 --- /dev/null +++ b/keydialog.cpp @@ -0,0 +1,157 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "keydialog.h" +#include +#include +#include +#include +#include + +KeyDialog::KeyDialog(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f) +{ + ui = new Ui_KeyDialog(); + ui->setupUi(this); + ui->mainLabelLineEdit->installEventFilter(this); + ui->secondLabelLineEdit->installEventFilter(this); + setModal(true); + resetCurrentItems(); + connect(ui->styleChooser, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), + this, SLOT(styleChanged(QListWidgetItem*))); + foreach(QAbstractButton* button, ui->buttonBox->buttons()) + button->setFocusPolicy(Qt::NoFocus); + styleDialog = new StyleDialog(); + connect(ui->addStyleButton, SIGNAL(pressed()), SLOT(addStyle())); + connect(ui->editStyleButton, SIGNAL(pressed()), SLOT(editStyle())); + connect(ui->deleteStyleButton, SIGNAL(pressed()), SLOT(deleteStyle())); +} + +void KeyDialog::styleChanged(QListWidgetItem* item) +{ + if (focusChanging) + return; + if (ui->secondLabelLineEdit->hasFocus()) + currentItems[1] = item; + else + currentItems[0] = item; +} + +int KeyDialog::exec(KeyItem* key) +{ + setWindowTitle(tr("Set labels for ") + key->keyId); + if (key == nullptr || ui->styleChooser->count() == 0) + return QDialog::Rejected; + keyItem = key; + ui->mainLabelLineEdit->setFocus(); + if (keyItem->labelTop != "") { + ui->styleChooser->setCurrentText(key->style[0]); + currentItems[0] = ui->styleChooser->findItem(key->style[0]); + } + if (keyItem->labelTop != "") + currentItems[1] = ui->styleChooser->findItem(key->style[1]); + + ui->mainLabelLineEdit->setText(key->labelTop); + ui->secondLabelLineEdit->setText(key->labelBottom); + + int result = QDialog::exec(); + if (result == Accepted) + { + keyItem->labelTop = ui->mainLabelLineEdit->text(); + keyItem->labelBottom = ui->secondLabelLineEdit->text(); + keyItem->setStyle(currentItems[0]->text(), 0); + if (keyItem->labelBottom != "") + keyItem->setStyle(currentItems[1]->text(), 1); + keyItem->updateItems(); + } + return result; +} + +bool KeyDialog::eventFilter(QObject* obj, QEvent* event) +{ + if (obj == ui->mainLabelLineEdit || obj == ui->secondLabelLineEdit) + { + if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) + { + QKeyEvent * keyEvent = static_cast(event); + if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up) + return QApplication::sendEvent(ui->styleChooser, event); + } + if (event->type() == QEvent::FocusOut) + { + focusChanging = true; + if (obj == ui->mainLabelLineEdit) + currentItems[0] = ui->styleChooser->currentItem(); + else + currentItems[1] = ui->styleChooser->currentItem(); + } + if (event->type() == QEvent::FocusIn) + { + if (obj == ui->mainLabelLineEdit) + { + qDebug() << currentItems[0]; + qDebug() << currentItems[0]->data(Qt::DisplayRole).toString(); + ui->styleChooser->setCurrentItem(currentItems[0]); + } + else + ui->styleChooser->setCurrentItem(currentItems[1]); + focusChanging = false; + } + } + return QDialog::eventFilter(obj, event); +} + +void KeyDialog::resetCurrentItems() +{ + currentItems[0] = currentItems[1] = ui->styleChooser->getDefault(); +} + +void KeyDialog::addStyle() +{ + int result = styleDialog->exec(StyleModel::model->styles[ui->styleChooser->currentItem()->text()], false); + if (result == Accepted) + { + Style *style = new Style(styleDialog->style); + StyleModel::model->styles[style->name] = style; + ui->styleChooser->updateStyles(); + } +} + +void KeyDialog::deleteStyle() +{ + QString name = ui->styleChooser->currentItem()->text(); + if (QMessageBox::question(this, "Really Delete?", "Really delete style " + name) + == QMessageBox::Yes) { + delete StyleModel::model->styles.take(name); + ui->styleChooser->updateStyles(); + } +} + +void KeyDialog::editStyle() +{ + Style *edStyle = StyleModel::model->styles[ui->styleChooser->currentItem()->text()]; + int result = styleDialog->exec(edStyle, true); + if (result == Accepted) + { + edStyle->operator==(styleDialog->style); + // TODO stuff if name changes + ui->styleChooser->updateStyles(); + } +} + + diff --git a/keydialog.h b/keydialog.h new file mode 100644 index 0000000..9121465 --- /dev/null +++ b/keydialog.h @@ -0,0 +1,53 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef KEYDIALOG_H +#define KEYDIALOG_H + +#include +#include +#include "keyitemmodel.h" +#include "styledialog.h" + +class KeyDialog : public QDialog +{ + Q_OBJECT +public: + KeyDialog(QWidget* parent = 0, Qt::WindowFlags f = 0); + int exec(KeyItem* key); + StyleChooser* getStyleChooser() {return ui->styleChooser;}; + +public slots: + void styleChanged(QListWidgetItem* item); + void stylesChanged() {ui->styleChooser->updateStyles();}; + void resetCurrentItems(); + void addStyle(); + void editStyle(); + void deleteStyle(); + +protected: + virtual bool eventFilter(QObject* obj, QEvent* event); + Ui_KeyDialog* ui; + KeyItem *keyItem; + QListWidgetItem * currentItems[2]; + bool focusChanging = false; + StyleDialog *styleDialog; +}; + +#endif // KEYDIALOG_H diff --git a/keydialog.ui b/keydialog.ui new file mode 100644 index 0000000..4b5f3a1 --- /dev/null +++ b/keydialog.ui @@ -0,0 +1,150 @@ + + + KeyDialog + + + + 0 + 0 + 506 + 368 + + + + Dialog + + + + + + + + + + &Main Label: + + + mainLabelLineEdit + + + + + + + + + + Second &Label + + + secondLabelLineEdit + + + + + + + + + + + + + + Qt::NoFocus + + + + + + + + + Qt::NoFocus + + + &Add + + + + + + + Qt::NoFocus + + + &Edit + + + + + + + Qt::NoFocus + + + &Delete + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + StyleChooser + QListWidget +
stylechooser.h
+
+
+ + + + buttonBox + accepted() + KeyDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + KeyDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +
diff --git a/keyitemmodel.cpp b/keyitemmodel.cpp index 86ed089..6e05b87 100644 --- a/keyitemmodel.cpp +++ b/keyitemmodel.cpp @@ -25,11 +25,8 @@ #include #include -QMap< QString, QPair > KeyItemModel::colors; - KeyItemModel::KeyItemModel() { - loadColors(); //loadCodeToIdMap(); } @@ -89,9 +86,9 @@ QVariant KeyItemModel::data(const QModelIndex& index, int role) const case Qt::ForegroundRole: switch (index.column()) { case 1: - return getBrushV(item->style[0], role); + return StyleModel::model->getBrushV(item->style[0], role); case 2: - return getBrushV(item->style[1], role); + return StyleModel::model->getBrushV(item->style[1], role); default: return QVariant(); } @@ -134,45 +131,6 @@ Qt::ItemFlags KeyItemModel::flags(const QModelIndex& index) const return flags; } -void KeyItemModel::loadColors() -{ - addColor("default", Qt::black, Qt::white); - addColor("black", Qt::white, Qt::black); - addColor("lightgray", Qt::black, Qt::lightGray); - addColor("darkgray", Qt::white, Qt::darkGray); - addColor("brownish", Qt::black, Qt::darkYellow); - addColor("cyan", Qt::black, Qt::cyan); - addColor("red", Qt::white, Qt::darkRed); - //addColor("", Qt::, Qt::); - emit stylesChanged(); -} - -void KeyItemModel::addColor(QString name, QColor fg, QColor bg) -{ - colors.insert(name, QPair(fg, bg)); -} - -QVariant KeyItemModel::getBrushV(QString name, int role) const -{ - if (name == "") - return getBrushV("default", role); - QBrush brush; - if (role == Qt::ForegroundRole) - brush.setColor(colors[name].first); - else - brush.setColor(colors[name].second); - return QVariant(brush); -} - -QColor KeyItemModel::getColor(QString name, int role) -{ - if (name == "") - return getColor("default", role); - if (role == Qt::ForegroundRole) - return colors[name].first; - else - return colors[name].second; -} KeyItem* KeyItemModel::key(const QModelIndex& index) const { diff --git a/keyitemmodel.h b/keyitemmodel.h index 4e18897..5f14d4a 100644 --- a/keyitemmodel.h +++ b/keyitemmodel.h @@ -25,6 +25,8 @@ #include #include #include "qgraphicskeyitem.h" +#include "style.h" +#include "stylemodel.h" class QGraphicsKeyItem; class KeyItem { @@ -45,15 +47,11 @@ Q_OBJECT void setKeys(QMultiMap keys); ~KeyItemModel(); - static QMap< QString, QPair > colors; QMap< int, KeyItem* > codeToKeyItemMap; - QVariant getBrushV(QString name, int role) const; - static QColor getColor(QString name, int role); bool load(QString filename); signals: void keyChanged(KeyItem*); - void stylesChanged(); public slots: void save(QString filename); @@ -67,8 +65,6 @@ public slots: virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); virtual Qt::ItemFlags flags(const QModelIndex &index) const; - void loadColors(); - void addColor(QString name, QColor fg, QColor bg); QMap loadCodeToIdMap(); QList items; diff --git a/keymapping.xml b/keymapping.xml new file mode 100644 index 0000000..93eda51 --- /dev/null +++ b/keymapping.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/qgraphicskeyitem.cpp b/qgraphicskeyitem.cpp index 1cf3fec..a0640d7 100644 --- a/qgraphicskeyitem.cpp +++ b/qgraphicskeyitem.cpp @@ -138,7 +138,7 @@ void QGraphicsKeyItem::paintText(QString text, QPolygonF polygon, int index) return; - item->setDefaultTextColor(KeyItemModel::getColor(key->style[index], Qt::ForegroundRole)); + item->setDefaultTextColor(StyleModel::model->getColor(key->style[index], Qt::ForegroundRole)); QFont font = item->font(); qreal size = polygon.boundingRect().height()/2.2; font.setPointSizeF(size); @@ -155,9 +155,9 @@ void QGraphicsKeyItem::paintText(QString text, QPolygonF polygon, int index) void QGraphicsKeyItem::updateContent() { - upperBrush.setColor(KeyItemModel::getColor(key->style[0], Qt::BackgroundRole)); + upperBrush.setColor(StyleModel::model->getColor(key->style[0], Qt::BackgroundRole)); upperBrush.setStyle(Qt::SolidPattern); - lowerBrush.setColor(KeyItemModel::getColor(key->style[1], Qt::BackgroundRole)); + lowerBrush.setColor(StyleModel::model->getColor(key->style[1], Qt::BackgroundRole)); lowerBrush.setStyle(Qt::SolidPattern); update(); diff --git a/style.cpp b/style.cpp new file mode 100644 index 0000000..45ebbb9 --- /dev/null +++ b/style.cpp @@ -0,0 +1,64 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "style.h" + +Style::Style() +{ + Style("default"); +} + +Style::Style(QString name, QColor fgCol, QColor bgCol) +{ + this->name = name; + fg.setColor(fgCol); + bg.setColor(bgCol); + bg.setStyle(Qt::SolidPattern); +} + + +Style::Style(const Style& other) +{ + +} + +bool Style::operator==(const Style& other) const +{ + +} + +void Style::setSize(qreal size) +{ + font.setPointSizeF(size); +} + +void Style::paint(QPainter& painter) +{ + +} + +void Style::setFont(QString f) +{ + +} + +void Style::setFont(QFont f) +{ + +} diff --git a/style.h b/style.h new file mode 100644 index 0000000..befaecb --- /dev/null +++ b/style.h @@ -0,0 +1,44 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef STYLE_H +#define STYLE_H +#include +#include +#include +#include + +class Style +{ +public: + Style(); + Style(QString name, QColor fgCol = Qt::black, QColor bgCol = Qt::white); + Style(const Style& other); + void setFont(QFont f); + void setFont(QString f); + void setSize(qreal size); + bool operator==(const Style& other) const; + void paint(QPainter &painter); // area + QString name; + QPen fg; + QBrush bg; + QFont font; +}; + +#endif // STYLE_H diff --git a/stylechooser.cpp b/stylechooser.cpp index 7a8697f..bce4b41 100644 --- a/stylechooser.cpp +++ b/stylechooser.cpp @@ -27,80 +27,74 @@ KeyStyleDelegate::KeyStyleDelegate(QObject* parent): QItemDelegate(parent) void KeyStyleDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - int margin = 5; - painter->save(); - QPen pen = index.data(Qt::ForegroundRole).value(); - pen.setWidthF(1.0); - painter->setPen(pen); - QBrush brush = index.data(Qt::BackgroundRole).value(); - - // if Selected... - if (option.state & QStyle::State_Selected) - margin = 1; - - painter->setBrush(brush); - - // Background - painter->drawRect(margin,margin + index.row()*HEIGHT, - ((StyleChooser*)parent())->width() - 2*margin,HEIGHT - margin*2); - // Text - pen.setStyle(Qt::SolidLine); - painter->drawText(10,index.row()*HEIGHT + 18, index.data().toString()); - - painter->restore(); + int margin = 5; + painter->save(); + QPen pen = index.data(Qt::ForegroundRole).value(); + pen.setWidthF(1.0); + painter->setPen(pen); + QBrush brush = index.data(Qt::BackgroundRole).value(); + + // if Selected... + if (option.state & QStyle::State_Selected) + margin = 1; + + painter->setBrush(brush); + + // Background + painter->drawRect(margin,margin + index.row()*HEIGHT, + ((StyleChooser*)parent())->width() - 2*margin,HEIGHT - margin*2); + // Text + pen.setStyle(Qt::SolidLine); + painter->drawText(10,index.row()*HEIGHT + 18, index.data().toString()); + + painter->restore(); } QSize KeyStyleDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { - return QSize(((StyleChooser*)parent())->width(), HEIGHT); + return QSize(((StyleChooser*)parent())->width(), HEIGHT); } StyleChooser::StyleChooser(QWidget* parent) : QListWidget(parent) { - if (parent == nullptr) { - setWindowFlags(Qt::Tool | Qt::FramelessWindowHint); - setWindowTitle("stylechooser"); - } - setItemDelegate(new KeyStyleDelegate(this)); - defaultItem = addStyle("default", QPair(Qt::black, Qt::white)); + if (parent == nullptr) { + setWindowFlags(Qt::Tool | Qt::FramelessWindowHint); + setWindowTitle("stylechooser"); + } + setItemDelegate(new KeyStyleDelegate(this)); + defaultItem = addStyle(new Style()); } void StyleChooser::updateStyles() { - clear(); - QListWidgetItem * item; - foreach (QString key, KeyItemModel::colors.keys()) { - item = addStyle(key, KeyItemModel::colors[key]); - if (key == "default") - defaultItem = item; - } + clear(); + QListWidgetItem * item; + foreach (QString key, StyleModel::model->styles.keys()) { + item = addStyle(StyleModel::model->styles[key]); + if (key == "default") + defaultItem = item; + } } -QListWidgetItem* StyleChooser::addStyle(QString name, QPair< QColor, QColor > colors) +QListWidgetItem* StyleChooser::addStyle(Style * style) { - QListWidgetItem *item = new QListWidgetItem(name, this); - QBrush bg; - QPen fg; - fg.setColor(colors.first); - //fg.setStyle(Qt::SolidPattern); - bg.setColor(colors.second); - bg.setStyle(Qt::SolidPattern); - item->setData(Qt::ForegroundRole, fg); - item->setData(Qt::BackgroundRole, bg); - return item; + QListWidgetItem *item = new QListWidgetItem(style->name, this); + item->setData(Qt::ForegroundRole, style->fg); + item->setData(Qt::BackgroundRole, style->bg); + return item; } QListWidgetItem* StyleChooser::findItem(QString name) { - QList list = findItems(name, Qt::MatchFixedString); - if (list.isEmpty()) - return getDefault(); - return list[0]; + QList list = findItems(name, Qt::MatchFixedString); + if (list.isEmpty()) + return getDefault(); + return list[0]; } void StyleChooser::setCurrentText(QString text) { - setCurrentItem(findItem(text)); + setCurrentItem(findItem(text)); } diff --git a/stylechooser.h b/stylechooser.h index 3ec902a..805d959 100644 --- a/stylechooser.h +++ b/stylechooser.h @@ -44,12 +44,13 @@ class StyleChooser : public QListWidget public: explicit StyleChooser(QWidget* parent = 0); QListWidgetItem* getDefault() {return defaultItem;}; - QListWidgetItem* addStyle(QString name, QPair colors); + QListWidgetItem* addStyle(Style *style); QListWidgetItem* findItem(QString name); public slots: void updateStyles(); void setCurrentText(QString text); protected: QListWidgetItem* defaultItem; + QMap itemMap; }; diff --git a/styledialog.cpp b/styledialog.cpp new file mode 100644 index 0000000..5fceed5 --- /dev/null +++ b/styledialog.cpp @@ -0,0 +1,98 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "styledialog.h" +#include + +StyleDialog::StyleDialog() +{ + ui = new Ui_StyleDialog(); + ui->setupUi(this); + setModal(true); + ui->preView->setScene(new QGraphicsScene(ui->preView)); + it = ui->preView->scene()->addText(tr("Preview")); + fgDialog = new QColorDialog(this); + fgDialog->setWindowTitle(tr("Text Color")); + connect(fgDialog, SIGNAL(currentColorChanged(QColor)), SLOT(changeFG(QColor))); + connect(ui->fontColorButton, SIGNAL(pressed()), fgDialog, SLOT(show())); + + bgDialog = new QColorDialog(this); + bgDialog->setWindowTitle(tr("Background Color")); + connect(bgDialog, SIGNAL(currentColorChanged(QColor)), SLOT(changeBG(QColor))); + connect(ui->bgColorButton, SIGNAL(pressed()), bgDialog, SLOT(show())); + + connect(ui->fontComboBox, SIGNAL(currentFontChanged(QFont)), SLOT(changFont(QFont))); + connect(ui->fontSizeSpinBox, SIGNAL(valueChanged(double)), SLOT(changeSize(double))); +} + +StyleDialog::~StyleDialog() +{ + delete fgDialog; + delete bgDialog; + delete ui; // TODO find out if its auto deleted +} + +int StyleDialog::exec(Style *styleToEdit, bool edit) +{ + style = (*styleToEdit); + if (!edit) + { + style.name = "New Style"; + setWindowTitle(tr("Create new style ")); + } + else + setWindowTitle(tr("Set up style ") + style.name); + ui->nameEdit->setText(style.name); + fgDialog->setCurrentColor(style.fg.color()); + bgDialog->setCurrentColor(style.bg.color()); + ui->fontComboBox->setCurrentFont(style.font); + ui->fontSizeSpinBox->setValue(style.font.pointSizeF()); + QDialog::exec(); +} + +void StyleDialog::changeBG(QColor color) +{ + style.bg.setColor(color); + ui->preView->setBackgroundBrush(style.bg); +} + +void StyleDialog::changeFG(QColor color) +{ + style.fg.setColor(color); + it->setDefaultTextColor(color); +} + +void StyleDialog::changFont(QFont font) +{ + font.setPointSizeF(ui->fontSizeSpinBox->value()); + style.font = font; + it->setFont(font); +} + +void StyleDialog::changeSize(double size) +{ + style.font.setPointSizeF(size); + it->setFont(style.font); +} + +void StyleDialog::changeName(QString& name) +{ + style.name = name; +} + diff --git a/styledialog.h b/styledialog.h new file mode 100644 index 0000000..34f1af3 --- /dev/null +++ b/styledialog.h @@ -0,0 +1,51 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef STYLEDIALOG_H +#define STYLEDIALOG_H + +#include +#include +#include +#include +#include "style.h" + +class StyleDialog : public QDialog +{ + Q_OBJECT +public: + StyleDialog(); + virtual ~StyleDialog(); + int exec(Style *styleToEdit, bool edit); + Style style; + +public slots: + void changeFG(QColor color); + void changeBG(QColor color); + void changFont(QFont font); + void changeSize(double size); + void changeName(QString& name); + +protected: + Ui_StyleDialog *ui; + QGraphicsTextItem * it; + QColorDialog *fgDialog, *bgDialog; +}; + +#endif // STYLEDIALOG_H diff --git a/styledialog.ui b/styledialog.ui new file mode 100644 index 0000000..6045481 --- /dev/null +++ b/styledialog.ui @@ -0,0 +1,153 @@ + + + StyleDialog + + + + 0 + 0 + 406 + 234 + + + + Dialog + + + + + + + + &Name + + + nameEdit + + + + + + + + + + + + + + Fon&t + + + fontComboBox + + + + + + + + + + + + + + Ma&x Font Size + + + fontSizeSpinBox + + + + + + + + + + + + + + + + + + + &Font Color + + + + + + + &Background Color + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + StyleDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + StyleDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/stylemodel.cpp b/stylemodel.cpp new file mode 100644 index 0000000..a52f4cf --- /dev/null +++ b/stylemodel.cpp @@ -0,0 +1,112 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "stylemodel.h" + +StyleModel *StyleModel::model = new StyleModel(); + +StyleModel::StyleModel() +{ + loadColors(); +} + +StyleModel::~StyleModel() +{ + +} + +void StyleModel::loadColors() +{ + addStyle("default", Qt::black, Qt::white); + addStyle("black", Qt::white, Qt::black); + addStyle("lightgray", Qt::black, Qt::lightGray); + addStyle("darkgray", Qt::white, Qt::darkGray); + addStyle("brownish", Qt::black, Qt::darkYellow); + addStyle("cyan", Qt::black, Qt::cyan); + addStyle("red", Qt::white, Qt::darkRed); + //addColor("", Qt::, Qt::); + emit stylesChanged(All, nullptr); +} + +void StyleModel::addStyle(QString name, QColor fg, QColor bg) +{ + while (styles.contains(name)) + name += "_"; + Style *newStyle = new Style(name, fg, bg); + styles.insert(name, newStyle); + stylesByPointer[newStyle] = name; + emit stylesChanged(StyleModel::New, newStyle); +} + +QVariant StyleModel::getBrushV(QString name, int role) const +{ + if (name == "") + return getBrushV("default", role); + QBrush brush; + if (role == Qt::ForegroundRole) + brush.setColor(styles[name]->fg.color()); + else + brush = styles[name]->bg; + return QVariant(brush); +} + +QColor StyleModel::getColor(QString name, int role) +{ + if (name == "") + return getColor("default", role); + if (role == Qt::ForegroundRole) + return styles[name]->fg.color(); + else + return styles[name]->bg.color(); +} + +void StyleModel::deleteStyle(Style* style) +{ + if (style->name == "default") + return; + if (styles[style->name] == style) + delete styles.take(style->name); + else { + QString name = stylesByPointer[style]; + delete styles.take(name); + } + stylesByPointer.remove(style); + emit stylesChanged(StyleModel::Delete, style); +} + +bool StyleModel::styleChangedOk(Style* style) +{ + if (styles[style->name] == style) { + emit stylesChanged(StyleModel::Edit, style); + return true; + } + bool result = true; + while (styles.contains(style->name)) { + result = false; + style->name += "_"; + } + + QString name = stylesByPointer.take(style); + styles.remove(name); + styles[name] = style; + stylesByPointer[style] = name; + emit stylesChanged(StyleModel::Name, style); + return result; +} + diff --git a/stylemodel.h b/stylemodel.h new file mode 100644 index 0000000..ac8720d --- /dev/null +++ b/stylemodel.h @@ -0,0 +1,53 @@ +/* + * + * Copyright (C) 2016 Arek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef STYLEMODEL_H +#define STYLEMODEL_H + +#include +#include +#include +#include +#include "style.h" + +class StyleModel : public QObject +{ + Q_OBJECT +public: + static StyleModel *model; + StyleModel(); + ~StyleModel(); + QVariant getBrushV(QString name, int role) const; + QColor getColor(QString name, int role); + void addStyle(QString name, QColor fg, QColor bg); + bool styleChangedOk(Style * style); + void deleteStyle(Style * style); + + QMap< QString, Style*> styles; + QMap< Style*, QString> stylesByPointer; + enum changeType{All, Edit, Name, New, Delete}; + +signals: + void stylesChanged(changeType type, Style* style); + +protected: + void loadColors(); +}; + +#endif // STYLEMODEL_H