Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Commit

Permalink
* Fix: reduce delay for initial display of Preferences dialog drastic…
Browse files Browse the repository at this point in the history
…ally (#308)
  • Loading branch information
bylee20 committed Apr 14, 2015
1 parent a8605c0 commit 858471d
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 76 deletions.
9 changes: 9 additions & 0 deletions src/bomi/player/mainwindow_m.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,16 +681,25 @@ auto MainWindow::Data::plugMenu() -> void
sview->setVisible(!sview->isVisible());
});
connect(tool[u"pref"_q], &QAction::triggered, p, [this] () {
QTime time;
int t1 = 0, t2 = 0, t3 = 0, t4 = 0;
if (!prefDlg) {
time.start();
prefDlg = dialog<PrefDialog>();
t1 = time.restart();
prefDlg->setAudioDeviceList(e.audioDeviceList());
t2 = time.restart();
connect(prefDlg.data(), &PrefDialog::applyRequested, p,
[this] { prefDlg->get(&pref); applyPref(); });
}
if (!prefDlg->isVisible()) {
time.restart();
prefDlg->set(&pref);
t3 = time.restart();
prefDlg->show();
t4 = time.restart();
}
qDebug() << t1 << t2 << t3 << t4;
});
connect(tool[u"associate-files"_q], &QAction::triggered,
p, [=] () { dialog<FileAssocDialog>()->exec(); });
Expand Down
26 changes: 21 additions & 5 deletions src/bomi/pref/prefdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "misc/simplelistmodel.hpp"
#include "ui_prefdialog.h"
#include <QQmlProperty>
#include <QElapsedTimer>

#ifdef None
#undef None
Expand All @@ -30,6 +31,7 @@ struct PrefDialog::Data {
QSet<ValueWatcher*> modified;
QHash<QObject*, QList<ValueWatcher*>> editorToWatcher;
Pref orig;
bool filling = false;

auto retranslate() -> void
{
Expand All @@ -39,9 +41,8 @@ struct PrefDialog::Data {
auto fillEditors(const Pref *pref) -> void
{
for (auto &w : watchers) {
if (!w.editor.isValid())
continue;
w.editor.write(w.property.read(pref));
if (w.editor.isValid())
w.editor.write(w.property.read(pref));
}
}

Expand Down Expand Up @@ -133,7 +134,7 @@ PrefDialog::PrefDialog(QWidget *parent)
addPage(tr("Mouse actions"), d->ui.ui_mouse, u":/img/input-mouse-32.png"_q);
addPage(tr("Control step"), d->ui.ui_step, u":/img/run-build-32.png"_q);

d->ui.app_fixed_font->setFixedFont(true);
d->ui.app_fixed_font->setFixedFontOnly(true);
d->ui.enable_hwaccel->setEnabled(OS::hwAcc()->isAvailable());
d->ui.screensaver_method->addItems(OS::screensaverMethods());
d->ui.screensaver_method->setVisible(d->ui.screensaver_method->count() > 1);
Expand Down Expand Up @@ -271,6 +272,14 @@ PrefDialog::PrefDialog(QWidget *parent)
auto &mo = Pref::staticMetaObject;
d->watchers.resize(mo.propertyCount() - mo.propertyOffset());

// 526
// remove(17); // skin
// 395
// remove(16); // osd
// 284
// remove(12); // sub_appearance
// 120

auto invoke = [&] (const char *funcName) -> QString
{
QString ret;
Expand Down Expand Up @@ -341,6 +350,8 @@ PrefDialog::~PrefDialog() {

auto PrefDialog::checkModified() -> void
{
if (d->filling)
return;
auto ws = d->editorToWatcher.value(sender());
if (ws.isEmpty())
return;
Expand All @@ -351,10 +362,12 @@ auto PrefDialog::checkModified() -> void
else
d->modified.remove(w);
}
setWindowModified(!d->modified.isEmpty());
if (isWindowModified() != !d->modified.isEmpty())
setWindowModified(!d->modified.isEmpty());
}

auto PrefDialog::setAudioDeviceList(const QList<AudioDevice> &devices) -> void {
return;
d->ui.audio_device->clear();
for (auto &dev : devices)
d->ui.audio_device->addItem(dev.name, dev.description);
Expand All @@ -366,8 +379,10 @@ auto PrefDialog::setAudioDeviceList(const QList<AudioDevice> &devices) -> void {

auto PrefDialog::set(const Pref *p) -> void
{
d->filling = true;
d->fillEditors(p);
d->sync();
d->filling = false;
}

auto PrefDialog::get(Pref *p) -> void
Expand All @@ -393,4 +408,5 @@ auto PrefDialog::changeEvent(QEvent *event) -> void
auto PrefDialog::showEvent(QShowEvent *event) -> void
{
QDialog::showEvent(event);
d->ui.stack->show();
}
98 changes: 74 additions & 24 deletions src/bomi/pref/prefdialog_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "tmp/algorithm.hpp"
#include "enum/mousebehavior.hpp"
#include <QHeaderView>
#include <QElapsedTimer>
#include <QScrollBar>

PrefMenuTreeItem::PrefMenuTreeItem(QAction *action, QVector<ActionInfo> &list, PrefMenuTreeItem *parent)
: QTreeWidgetItem(parent, action->menu() ? Menu : action->isSeparator() ? Separator : Action)
Expand Down Expand Up @@ -75,10 +77,9 @@ auto PrefMenuTreeItem::reset() -> void
emitDataChanged();
}

auto PrefMenuTreeItem::setShortcut(const Shortcut &s) -> void
auto PrefMenuTreeItem::setShortcut(const Shortcut &s) -> bool
{
if (_Change(m_shortcut, s))
emitDataChanged();
return _Change(m_shortcut, s);
}

/******************************************************************************/
Expand All @@ -95,6 +96,26 @@ PrefMenuTreeWidget::PrefMenuTreeWidget(QWidget *parent)
connect(this, &QTreeWidget::itemChanged, this, &PrefMenuTreeWidget::changed);
}

auto PrefMenuTreeWidget::set(const ShortcutMap &map) -> void {
bool changed = false;
const int h = horizontalScrollBar()->value();
const int v = verticalScrollBar()->value();
QList<QTreeWidgetItem*> expanded;
for_recursive([&] (auto i) {
changed |= i->setShortcut(map.shortcut(i->id()));
if (i->isExpanded())
expanded.push_back(i);
});
if (changed) {
reset();
for (auto i : expanded)
i->setExpanded(true);
horizontalScrollBar()->setValue(h);
verticalScrollBar()->setValue(v);
emit this->changed();
}
}

/******************************************************************************/

auto PrefDelegate::sizeHint(const QStyleOptionViewItem& option,
Expand Down Expand Up @@ -152,9 +173,6 @@ auto PrefDelegate::drawHeader(QPainter *painter, const QRect &rect,

/******************************************************************************/

static constexpr auto ModRole = Qt::UserRole + 1;
static constexpr auto ActionIndexRole = Qt::UserRole + 2;

class MouseActionDelegate : public QStyledItemDelegate {
const QVector<ActionInfo> *actions = nullptr;
public:
Expand All @@ -175,7 +193,7 @@ class MouseActionDelegate : public QStyledItemDelegate {
{
if (!editor)
return;
const int idx = index.model()->data(index, ActionIndexRole).toInt();
const int idx = index.model()->data(index, Qt::EditRole).toInt();
auto combox = static_cast<QComboBox*>(editor);
combox->setCurrentIndex(idx);
}
Expand All @@ -186,8 +204,7 @@ class MouseActionDelegate : public QStyledItemDelegate {
return;
auto combo = static_cast<QComboBox*>(editor);
const auto idx = combo->currentIndex();
model->setData(index, idx, ActionIndexRole);
model->setData(index, actions->at(idx).desc);
model->setData(index, idx, Qt::EditRole);
}
auto updateEditorGeometry(QWidget *w, const QStyleOptionViewItem &opt,
const QModelIndex &/*index*/) const -> void
Expand All @@ -203,7 +220,37 @@ static const auto mods = QList<KeyModifier>() << KeyModifier::None
<< KeyModifier::Alt;

class MouseActionItem : public QTreeWidgetItem {

struct ChildItem : public QTreeWidgetItem {
KeyModifier modifier;
const QVector<ActionInfo> *actions = nullptr;
int index = 0;
auto data(int column, int role) const -> QVariant final
{
switch (column) {
case 0:
if (role != Qt::DisplayRole)
return QVariant();
if (modifier == KeyModifier::None)
return qApp->translate("PrefDialog", "No modifier");
return Key((int)modifier).toString(Key::NativeText);
case 1:
if (role == Qt::DisplayRole)
return actions ? actions->at(index).desc : QVariant();
if (role == Qt::EditRole)
return index;
return QVariant();
default:
return QVariant();
}
}
auto setData(int column, int role, const QVariant &value) -> void final
{
if (column != 1 || role != Qt::EditRole || !actions)
return;
if (_Change(index, qBound(0, value.toInt(), actions->size() - 1)))
emitDataChanged();
}
};
public:
MouseActionItem(MouseBehavior mb, const QVector<ActionInfo> *actions,
QTreeWidget *parent)
Expand All @@ -212,13 +259,9 @@ class MouseActionItem : public QTreeWidgetItem {
setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
setText(0, MouseBehaviorInfo::description(mb));
for (auto mod : mods) {
auto sub = new QTreeWidgetItem;
sub->setData(0, ModRole, QVariant::fromValue(mod));
if (mod != KeyModifier::None) {
const QKeySequence key((int)mod);
sub->setText(0, key.toString(QKeySequence::NativeText));
} else
sub->setText(0, qApp->translate("PrefDialog", "No modifier"));
auto sub = new ChildItem;
sub->modifier = mod;
sub->actions = m_actions;
sub->setFlags(flags() | Qt::ItemIsEditable);
addChild(sub);
}
Expand All @@ -227,14 +270,15 @@ class MouseActionItem : public QTreeWidgetItem {
auto compare(const KeyModifierActionMap &map) const -> bool
{
for (int i = 0; i < mods.size(); ++i) {
const auto idx = child(i)->data(1, ActionIndexRole).toInt();
const auto idx = static_cast<const ChildItem*>(child(i))->index;
if (map[mods[i]] != m_actions->at(idx).id)
return false;
}
return true;
}
auto set(const KeyModifierActionMap &map) -> void
auto set(const KeyModifierActionMap &map) -> bool
{
bool changed = false;
for (int i = 0; i < mods.size(); ++i) {
const auto id = map[mods[i]];
int idx = 0;
Expand All @@ -244,16 +288,16 @@ class MouseActionItem : public QTreeWidgetItem {
break;
}
}
auto sub = child(i);
sub->setText(1, m_actions->at(idx).desc);
sub->setData(1, ActionIndexRole, idx);
auto c = static_cast<ChildItem*>(child(i));
changed |= _Change(c->index, idx);
}
return changed;
}
auto get() const -> KeyModifierActionMap
{
KeyModifierActionMap map;
for (int i = 0; i < mods.size(); ++i) {
const auto idx = child(i)->data(1, ActionIndexRole).toInt();
const auto idx = child(i)->data(1, Qt::EditRole).toInt();
map[mods[i]] = m_actions->at(idx).id;
}
return map;
Expand Down Expand Up @@ -316,10 +360,16 @@ auto PrefMouseActionTree::compare(const QVariant &var) const -> bool

auto PrefMouseActionTree::set(const MouseActionMap &map) -> void
{
bool changed = false;
for (int i = 0; i < topLevelItemCount(); ++i) {
const auto item = static_cast<MouseActionItem*>(topLevelItem(i));
item->set(map[item->behavior()]);
changed |= item->set(map[item->behavior()]);
}
if (!changed)
return;
reset();
expandAll();
emit this->changed();
}

auto PrefMouseActionTree::get() const -> MouseActionMap
Expand Down
10 changes: 7 additions & 3 deletions src/bomi/pref/prefdialog_p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QAction>
#include <QStyledItemDelegate>
#include <QTreeWidgetItem>
#include <QElapsedTimer>

class Menu;

Expand All @@ -33,7 +34,7 @@ class PrefMenuTreeItem : public QTreeWidgetItem {
auto isSeparator() const -> bool { return type() == Separator; }
auto key(int i) const -> Key {return m_shortcut.key(i);}
auto setKey(int idx, const QKeySequence &key) -> void;
auto setShortcut(const Shortcut &s) -> void;
auto setShortcut(const Shortcut &s) -> bool;
auto shortcut() const -> Shortcut { return m_shortcut; }
auto contains(const Key &key) -> bool { return m_shortcut.contains(key); }
auto id() const -> QString { return m_id; }
Expand All @@ -46,15 +47,16 @@ class PrefMenuTreeItem : public QTreeWidgetItem {
QString m_desc, m_name, m_id; Shortcut m_shortcut;
};



class PrefMenuTreeWidget : public QTreeWidget {
Q_OBJECT
Q_PROPERTY(ShortcutMap value READ get WRITE set NOTIFY changed)
using Item = PrefMenuTreeItem;
public:
PrefMenuTreeWidget(QWidget *parent = nullptr);
auto actionInfoList() const -> const QVector<ActionInfo>& { return m_actionInfos; }
auto set(const ShortcutMap &map) -> void
{ for_recursive([&] (auto i) { i->setShortcut(map.shortcut(i->id())); }); }
auto set(const ShortcutMap &map) -> void;
auto get() -> ShortcutMap
{
ShortcutMap map;
Expand Down Expand Up @@ -126,6 +128,8 @@ class PrefMouseActionTree : public QTreeWidget {
auto compare(const QVariant &var) const -> bool;
signals:
void changed();
private:
QMap<QString, int> m_actIdx;
};

class PrefStepItem;
Expand Down
1 change: 1 addition & 0 deletions src/bomi/quick/osdthemeobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class OsdThemeWidget : public QWidget {
signals:
void valueChanged();
private:
auto emitChanged();
struct Data;
Data *d;
};
Expand Down
22 changes: 8 additions & 14 deletions src/bomi/ui/osdstylewidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="FontComboBox" name="font_family"/>
<widget class="FontComboBox" name="font_family">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_21">
Expand Down Expand Up @@ -67,19 +74,6 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_21">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="1" column="1">
Expand Down
Loading

0 comments on commit 858471d

Please sign in to comment.