openscad/src/FontListDialog.cc

147 lines
4.7 KiB
C++
Raw Permalink Normal View History

/*
* OpenSCAD (www.openscad.org)
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
* Marius Kintel <marius@kintel.net>
*
* 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 2 of the License, or
* (at your option) any later version.
*
* As a special exception, you have permission to link this program
* with the CGAL library and distribute executables, as long as you
* follow the requirements of the GNU GPL in regard to all of the
* software in the executable aside from CGAL.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QClipboard>
2014-07-12 06:11:16 +04:00
#include <QSortFilterProxyModel>
2014-10-19 00:25:25 +04:00
#include "qtgettext.h"
#include "FontListDialog.h"
#include "FontCache.h"
FontListDialog::FontListDialog()
{
model = NULL;
2014-07-12 06:11:16 +04:00
proxy = NULL;
setupUi(this);
connect(this->okButton, SIGNAL(clicked()), this, SLOT(accept()));
}
FontListDialog::~FontListDialog()
{
}
void FontListDialog::on_copyButton_clicked()
2014-07-12 03:57:11 +04:00
{
2014-07-12 06:11:16 +04:00
font_selected(selection);
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(selection);
2014-07-12 06:11:16 +04:00
}
void FontListDialog::on_filterLineEdit_textChanged(const QString &text)
{
proxy->setFilterWildcard(text);
2014-07-12 03:57:11 +04:00
}
2014-07-12 06:11:16 +04:00
void FontListDialog::selection_changed(const QItemSelection &current, const QItemSelection &)
2014-07-12 03:57:11 +04:00
{
2014-07-12 06:11:16 +04:00
if (current.count() == 0) {
copyButton->setEnabled(false);
2014-12-23 05:07:27 +03:00
tableView->setDragText("");
2014-07-12 06:11:16 +04:00
return;
2014-07-12 03:57:11 +04:00
}
2014-07-12 06:11:16 +04:00
const QModelIndex &idx = proxy->mapToSource(current.indexes().at(0));
const QString name = model->item(idx.row(), 0)->text();
const QString style = model->item(idx.row(), 1)->text();
selection = QString("\"%1:style=%2\"").arg(quote(name)).arg(quote(style));
copyButton->setEnabled(true);
2014-12-23 05:07:27 +03:00
tableView->setDragText(selection);
2014-07-12 03:57:11 +04:00
}
void FontListDialog::update_font_list()
{
copyButton->setEnabled(false);
2014-07-12 03:57:11 +04:00
2014-07-12 06:11:16 +04:00
if (proxy) {
delete proxy;
proxy = NULL;
}
if (model) {
delete model;
2014-07-12 06:11:16 +04:00
model = NULL;
}
FontInfoList *list = FontCache::instance()->list_fonts();
2014-07-12 03:57:11 +04:00
model = new QStandardItemModel(list->size(), 3, this);
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Font name")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Font style")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Filename")));
int idx = 0;
for (FontInfoList::iterator it = list->begin();it != list->end();it++, idx++) {
FontInfo font_info = (*it);
QStandardItem *family = new QStandardItem(QString(font_info.get_family().c_str()));
family->setEditable(false);
model->setItem(idx, 0, family);
QStandardItem *style = new QStandardItem(QString(font_info.get_style().c_str()));
style->setEditable(false);
model->setItem(idx, 1, style);
QStandardItem *file = new QStandardItem(QString(font_info.get_file().c_str()));
file->setEditable(false);
model->setItem(idx, 2, file);
}
2014-07-12 06:11:16 +04:00
proxy = new QSortFilterProxyModel(this);
proxy->setSourceModel(model);
proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
this->tableView->setModel(proxy);
2014-07-12 03:57:11 +04:00
this->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
this->tableView->sortByColumn(0, Qt::AscendingOrder);
this->tableView->resizeColumnsToContents();
this->tableView->setSortingEnabled(true);
2014-07-12 03:57:11 +04:00
connect(tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selection_changed(const QItemSelection &, const QItemSelection &)));
delete list;
}
/**
* Quote a string according to the requirements of font-config.
* See http://www.freedesktop.org/software/fontconfig/fontconfig-user.html
*
* The '\', '-', ':' and ',' characters in family names must be preceded
* by a '\' character to avoid having them misinterpreted. Similarly, values
* containing '\', '=', '_', ':' and ',' must also have them preceded by a
* '\' character. The '\' characters are stripped out of the family name and
* values as the font name is read.
*
* @param text unquoted string
* @return quoted text
*/
QString FontListDialog::quote(const QString& text)
{
QString result = text;
result.replace('\\', "\\\\\\\\")
.replace('-', "\\\\-")
.replace(':', "\\\\:")
.replace(',', "\\\\,")
.replace('=', "\\\\=")
.replace('_', "\\\\_");
return result;
}