Skip to content

Commit

Permalink
libaudqt: hook up a font entry widget
Browse files Browse the repository at this point in the history
  • Loading branch information
kaniini committed Nov 4, 2019
1 parent 1839fbd commit b25f6ac
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/libaudqt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SRCS = about-qt.cc \
equalizer-qt.cc \
file-entry.cc \
fileopener.cc \
font-entry.cc \
images.cc \
infopopup-qt.cc \
infowin-qt.cc \
Expand Down
195 changes: 195 additions & 0 deletions src/libaudqt/font-entry.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* font-entry.cc
* Copyright 2015 John Lindgren
* Copyright 2019 Ariadne Conill
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions, and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions, and the following disclaimer in the documentation
* provided with the distribution.
*
* This software is provided "as is" and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising from
* the use of this software.
*/

#include "libaudqt.h"

#include <QAction>
#include <QLineEdit>
#include <QPointer>
#include <QFontDialog>

#include <libaudcore/audstrings.h>
#include <libaudcore/i18n.h>

namespace audqt {

class FontEntry : public QLineEdit {
public:
FontEntry (QWidget * parent = nullptr, const char * font = nullptr) :
QLineEdit (parent),
m_action (get_icon ("dialog-text-and-font"), _("Set Font"), nullptr)

This comment has been minimized.

Copy link
@radioactiveman

radioactiveman Nov 6, 2019

Member

Can we use a more common icon here? I found no theme for Linux which contains an icon with this name.
My suggestion is to use preferences-system or preferences-desktop-font.
Both are standard icons listed at https://specifications.freedesktop.org/icon-naming-spec/latest/ar01s04.html

@jlindgren90 What is your opinion about this?

This comment has been minimized.

Copy link
@jlindgren90

jlindgren90 Nov 6, 2019

Member

+1 for preferences-desktop-font. We should also add a fallback under images/.

This comment has been minimized.

Copy link
@kaniini

kaniini Nov 7, 2019

Author Member

The icon I used is from Breeze. No objection to changing it though.

{
addAction (& m_action, TrailingPosition);
connect (& m_action, & QAction::triggered, this, & FontEntry::show_dialog);

if (font)
setText (font);

end (false);
}

private:
QFontDialog * create_dialog ();
void show_dialog ();

QAction m_action;
QPointer<QFontDialog> m_dialog;
};

/* parse a subset of Pango font descriptions */
static QFont * qfont_from_string (const char * name)
{
auto family = str_copy (name);
int size = 0;
QFont::Weight weight = QFont::Normal;
QFont::Style style = QFont::StyleNormal;
QFont::Stretch stretch = QFont::Unstretched;

while (1)
{
/* check for attributes */
bool attr_found = false;
const char * space = strrchr (family, ' ');

if (space)
{
const char * attr = space + 1;
int num = str_to_int (attr);

attr_found = true;

if (num > 0)
size = num;
else if (! strcmp (attr, "Light"))
weight = QFont::Light;
else if (! strcmp (attr, "Bold"))
weight = QFont::Bold;
else if (! strcmp (attr, "Oblique"))
style = QFont::StyleOblique;
else if (! strcmp (attr, "Italic"))
style = QFont::StyleItalic;
else if (! strcmp (attr, "Condensed"))
stretch = QFont::Condensed;
else if (! strcmp (attr, "Expanded"))
stretch = QFont::Expanded;
else
attr_found = false;
}

if (! attr_found)
{
auto font = new QFont ((const char *) family);

/* check for a recognized font family */
if (! space || font->exactMatch ())
{
if (size > 0)
font->setPointSize (size);
if (weight != QFont::Normal)
font->setWeight (weight);
if (style != QFont::StyleNormal)
font->setStyle (style);
if (stretch != QFont::Unstretched)
font->setStretch (stretch);

return font;
}

delete font;
}

family.resize (space - family);
}
}

QFontDialog * FontEntry::create_dialog ()
{
auto dialog = new QFontDialog (this);

QObject::connect (dialog, & QFontDialog::fontSelected, [this] (const QFont & font) {
auto family = font.family ().toUtf8 ();

// build the description string
StringBuf font_str = str_copy ((const char *) family);

auto weight = font.weight ();
auto style = font.style ();
auto stretch = font.stretch ();

if (weight == QFont::Light)
font_str = str_concat({font_str, " Light"});
else if (weight == QFont::Bold)
font_str = str_concat({font_str, " Bold"});

if (style == QFont::StyleOblique)
font_str = str_concat({font_str, " Oblique"});
else if (style == QFont::StyleItalic)
font_str = str_concat({font_str, " Italic"});

if (stretch == QFont::Condensed)
font_str = str_concat ({font_str, " Condensed"});
else if (stretch == QFont::Expanded)
font_str = str_concat ({font_str, " Expanded"});

font_str = str_concat({font_str, " ", int_to_str (font.pointSize ())});

font_entry_set_font (this, font_str);
});

return dialog;
}

void FontEntry::show_dialog ()
{
if (! m_dialog)
m_dialog = create_dialog ();

auto font = qfont_from_string (font_entry_get_font (this));
if (! font)
{
window_bring_to_front (m_dialog);
return;
}

m_dialog->setCurrentFont (*font);
delete font;

window_bring_to_front (m_dialog);
}

EXPORT QLineEdit * font_entry_new (QWidget * parent, const char * font)
{
return new FontEntry (parent, font);
}

EXPORT String font_entry_get_font (QLineEdit * entry)
{
QByteArray text = entry->text ().toUtf8 ();

return String (text);
}

EXPORT void font_entry_set_font (QLineEdit * entry, const char * font)
{
entry->setText (font);
entry->end (false);
}

} // namespace audqt
5 changes: 5 additions & 0 deletions src/libaudqt/libaudqt.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ QLineEdit * file_entry_new (QWidget * parent, const char * title,
String file_entry_get_uri (QLineEdit * entry);
void file_entry_set_uri (QLineEdit * entry, const char * uri);

/* font-entry.cc */
QLineEdit * font_entry_new (QWidget * parent, const char * font);
String font_entry_get_font (QLineEdit * entry);
void font_entry_set_font (QLineEdit * entry, const char * font);

/* prefs-builder.cc */
void prefs_populate (QBoxLayout * layout, ArrayRef<PreferencesWidget> widgets, const char * domain);

Expand Down
1 change: 1 addition & 0 deletions src/libaudqt/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ libaudqt_sources = [
'equalizer-qt.cc',
'file-entry.cc',
'fileopener.cc',
'font-entry.cc',
'infopopup-qt.cc',
'infowin-qt.cc',
'info-widget.cc',
Expand Down
5 changes: 4 additions & 1 deletion src/libaudqt/prefs-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,13 @@ void prefs_populate (QBoxLayout * layout, ArrayRef<PreferencesWidget> widgets, c
case PreferencesWidget::Entry:
/* TODO: implement file chooser and font selector */
case PreferencesWidget::FileEntry:
case PreferencesWidget::FontButton:
layout->addWidget (new StringWidget (& w, domain));
break;

case PreferencesWidget::FontButton:
layout->addWidget (new FontWidget (& w, domain));
break;

case PreferencesWidget::RadioButton:
{
if (! radio_btn_group[w.child])
Expand Down
25 changes: 25 additions & 0 deletions src/libaudqt/prefs-widget-qt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,31 @@ void StringWidget::update ()
m_lineedit->setText ((const char *) m_parent->cfg.get_string ());
}

/* font widget (audqt::FontEntry) */
FontWidget::FontWidget (const PreferencesWidget * parent, const char * domain) :
HookableWidget (parent, domain),
m_lineedit (font_entry_new (this, nullptr))
{
auto layout = make_hbox (this);

if (parent->label)
layout->addWidget (new QLabel (translate_str (parent->label, domain)));

layout->addWidget (m_lineedit, 1);

update ();

QObject::connect (m_lineedit, & QLineEdit::textChanged, [this] (const QString & value) {
if (! m_updating)
m_parent->cfg.set_string (value.toUtf8 ());
});
}

void FontWidget::update ()
{
m_lineedit->setText ((const char *) m_parent->cfg.get_string ());
}

/* combo box widget (string or int) */
ComboBoxWidget::ComboBoxWidget (const PreferencesWidget * parent, const char * domain) :
HookableWidget (parent, domain),
Expand Down
9 changes: 9 additions & 0 deletions src/libaudqt/prefs-widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ class StringWidget : public QWidget, HookableWidget {
QLineEdit * m_lineedit;
};

/* font widget (audqt::FontEntry) */
class FontWidget : public QWidget, HookableWidget {
public:
FontWidget (const PreferencesWidget * parent, const char * domain);
private:
void update ();
QLineEdit * m_lineedit;
};

/* combo box (string or int) */
class ComboBoxWidget : public QWidget, HookableWidget {
public:
Expand Down

0 comments on commit b25f6ac

Please sign in to comment.