WIP: delete function, installation dialog (first steps)

This commit is contained in:
Matthias Koefferlein 2017-03-25 00:08:17 +01:00
parent 10b09c3575
commit d160a5c27c
14 changed files with 941 additions and 197 deletions

View File

@ -0,0 +1,313 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SaltGrainInstallationDialog</class>
<widget class="QDialog" name="SaltGrainInstallationDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>692</width>
<height>440</height>
</rect>
</property>
<property name="windowTitle">
<string>Salt Mine Repository Browser</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QWidget" name="layoutWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="rightMargin">
<number>4</number>
</property>
<item>
<widget class="QFrame" name="frame_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Repository</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<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>
</widget>
</item>
<item>
<widget class="QFrame" name="frame_3">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QToolButton" name="mark_button">
<property name="text">
<string>Install</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>126</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="lay::DecoratedLineEdit" name="lineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QListView" name="salt_view">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="iconSize">
<size>
<width>64</width>
<height>64</height>
</size>
</property>
<property name="selectionRectVisible">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QFrame" name="details_frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Details</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="lay::SaltGrainDetailsTextWidget" name="details_text">
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="button_box">
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
<action name="actionNew">
<property name="icon">
<iconset resource="layResources.qrc">
<normaloff>:/add.png</normaloff>:/add.png</iconset>
</property>
<property name="text">
<string>New</string>
</property>
<property name="toolTip">
<string>New package</string>
</property>
</action>
<action name="actionDelete">
<property name="icon">
<iconset resource="layResources.qrc">
<normaloff>:/clear.png</normaloff>:/clear.png</iconset>
</property>
<property name="text">
<string>Delete</string>
</property>
<property name="toolTip">
<string>Delete package</string>
</property>
</action>
<action name="actionImport">
<property name="icon">
<iconset resource="layResources.qrc">
<normaloff>:/import.png</normaloff>:/import.png</iconset>
</property>
<property name="text">
<string>Import</string>
</property>
<property name="toolTip">
<string>Import package</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>lay::DecoratedLineEdit</class>
<extends>QLineEdit</extends>
<header>layWidgets.h</header>
</customwidget>
<customwidget>
<class>lay::SaltGrainDetailsTextWidget</class>
<extends>QTextBrowser</extends>
<header>laySaltGrainDetailsTextWidget.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>details_text</tabstop>
</tabstops>
<resources>
<include location="layResources.qrc"/>
</resources>
<connections>
<connection>
<sender>button_box</sender>
<signal>rejected()</signal>
<receiver>SaltGrainInstallationDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>559</x>
<y>425</y>
</hint>
<hint type="destinationlabel">
<x>576</x>
<y>437</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -52,7 +52,9 @@ HEADERS = \
laySaltManagerDialog.h \
laySaltGrainDetailsTextWidget.h \
laySaltGrainPropertiesDialog.h \
laySaltDownloadManager.h
laySaltDownloadManager.h \
laySaltModel.h \
laySaltGrainInstallationDialog.h
FORMS = \
ClipDialog.ui \
@ -100,7 +102,8 @@ FORMS = \
MainConfigPage7.ui \
SaltManagerDialog.ui \
SaltGrainPropertiesDialog.ui \
SaltGrainTemplateSelectionDialog.ui
SaltGrainTemplateSelectionDialog.ui \
SaltGrainInstallationDialog.ui
SOURCES = \
gsiDeclLayApplication.cc \
@ -150,7 +153,9 @@ SOURCES = \
laySaltManagerDialog.cc \
laySaltGrainDetailsTextWidget.cc \
laySaltGrainPropertiesDialog.cc \
laySaltDownloadManager.cc
laySaltDownloadManager.cc \
laySaltModel.cc \
laySaltGrainInstallationDialog.cc
RESOURCES = layBuildInMacros.qrc \
layHelpResources.qrc \

View File

@ -189,6 +189,7 @@ bool remove_from_collection (SaltGrains &collection, const std::string &name)
++gnext;
collection.remove_grain (g, true);
res = true;
g = gnext;
}
}
@ -207,9 +208,15 @@ Salt::remove_grain (const SaltGrain &grain)
{
tl::info << QObject::tr ("Removing package '%1' ..").arg (tl::to_qstring (grain.name ()));
if (remove_from_collection (m_root, grain.name ())) {
tl::info << QObject::tr ("Package '%1' removed.").arg (tl::to_qstring (grain.name ()));
// NOTE: this is a bit brute force .. we could as well try to insert the new grain into the existing structure
refresh ();
invalidate ();
return true;
} else {
tl::warn << QObject::tr ("Failed to remove package '%1'.").arg (tl::to_qstring (grain.name ()));
return false;

View File

@ -123,6 +123,31 @@ public:
*/
SaltGrain *grain_by_name (const std::string &name);
/**
* @brief Loads the salt from a "salt mine" file
*/
void load (const std::string &p)
{
m_root.load (p);
}
/**
* @brief Loads the salt from a "salt mine" stream
*/
void load (tl::InputStream &s)
{
m_root.load (s);
}
/**
* @brief Saves the salt to a "salt mine" file
* This feature is provided for debugging purposes mainly.
*/
void save (const std::string &p)
{
m_root.save (p);
}
/**
* @brief Removes a grain from the salt
*

View File

@ -297,7 +297,7 @@ struct ImageConverter
}
};
static tl::XMLStruct<lay::SaltGrain> xml_struct ("salt-grain",
static tl::XMLElementList s_xml_elements =
tl::make_member (&SaltGrain::name, &SaltGrain::set_name, "name") +
tl::make_member (&SaltGrain::version, &SaltGrain::set_version, "version") +
tl::make_member (&SaltGrain::title, &SaltGrain::set_title, "title") +
@ -315,8 +315,15 @@ static tl::XMLStruct<lay::SaltGrain> xml_struct ("salt-grain",
tl::make_member (&SaltGrain::Dependency::name, "name") +
tl::make_member (&SaltGrain::Dependency::url, "url") +
tl::make_member (&SaltGrain::Dependency::version, "version")
)
);
);
static tl::XMLStruct<lay::SaltGrain> s_xml_struct ("salt-grain", s_xml_elements);
tl::XMLElementList &
SaltGrain::xml_struct ()
{
return s_xml_elements;
}
bool
SaltGrain::is_readonly () const
@ -332,7 +339,7 @@ SaltGrain::load (const std::string &p)
if (p[0] != ':') {
tl::XMLFileSource source (p);
xml_struct.parse (source, *this);
s_xml_struct.parse (source, *this);
} else {
@ -346,7 +353,7 @@ SaltGrain::load (const std::string &p)
std::string str_data (data.constData (), data.size ());
tl::XMLStringSource source (str_data);
xml_struct.parse (source, *this);
s_xml_struct.parse (source, *this);
}
}
@ -355,7 +362,7 @@ void
SaltGrain::load (tl::InputStream &p)
{
tl::XMLStreamSource source (p);
xml_struct.parse (source, *this);
s_xml_struct.parse (source, *this);
}
void
@ -368,7 +375,7 @@ void
SaltGrain::save (const std::string &p) const
{
tl::OutputStream os (p, tl::OutputStream::OM_Plain);
xml_struct.write (os, *this);
s_xml_struct.write (os, *this);
}
SaltGrain

View File

@ -26,6 +26,7 @@
#include "layCommon.h"
#include "tlObject.h"
#include "tlStream.h"
#include "tlXMLParser.h"
#include <QTime>
#include <QImage>
@ -350,6 +351,11 @@ public:
*/
void save (const std::string &file_path) const;
/**
* @brief Gets the XML structure representing a grain
*/
static tl::XMLElementList &xml_struct ();
/**
* @brief Compares two version strings
* Returns -1 if v1 < v2, 0 if v1 == v2 and 1 if v1 > v2.

View File

@ -0,0 +1,101 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2017 Matthias Koefferlein
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "laySaltGrainInstallationDialog.h"
#include "laySaltModel.h"
#include "laySaltGrainPropertiesDialog.h"
#include "laySalt.h"
#include "ui_SaltGrainTemplateSelectionDialog.h"
#include "tlString.h"
#include "tlExceptions.h"
#include <QAbstractItemModel>
#include <QAbstractTextDocumentLayout>
#include <QStyledItemDelegate>
#include <QTextDocument>
#include <QPainter>
#include <QDir>
#include <QTextStream>
#include <QBuffer>
#include <QResource>
#include <QMessageBox>
namespace lay
{
// --------------------------------------------------------------------------------------
// SaltGrainInstallation implementation
SaltGrainInstallationDialog::SaltGrainInstallationDialog (QWidget *parent, lay::Salt *salt)
: QDialog (parent), mp_salt (salt)
{
Ui::SaltGrainInstallationDialog::setupUi (this);
// TODO: cache package list
// @@@
m_salt_mine.load ("/home/matthias/salt.mine");
// @@@
SaltModel *model = new SaltModel (this, &m_salt_mine);
salt_view->setModel (model);
salt_view->setItemDelegate (new SaltItemDelegate (this));
salt_view->setCurrentIndex (model->index (0, 0, QModelIndex ()));
connect (salt_view->selectionModel (), SIGNAL (currentChanged (const QModelIndex &, const QModelIndex &)), this, SLOT (current_changed ()));
connect (mark_button, SIGNAL (clicked ()), this, SLOT (mark ()));
connect (button_box->button (QDialogButtonBox::Apply), SIGNAL (clicked ()), this, SLOT (apply ()));
current_changed ();
}
void
SaltGrainInstallationDialog::current_changed ()
{
SaltGrain *g = current_grain ();
details_text->set_grain (g);
details_frame->setEnabled (g != 0);
}
lay::SaltGrain *
SaltGrainInstallationDialog::current_grain ()
{
SaltModel *model = dynamic_cast <SaltModel *> (salt_view->model ());
return model ? model->grain_from_index (salt_view->currentIndex ()) : 0;
}
void
SaltGrainInstallationDialog::apply ()
{
// @@@
}
void
SaltGrainInstallationDialog::mark ()
{
// @@@
}
}

View File

@ -0,0 +1,76 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2017 Matthias Koefferlein
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef HDR_laySaltGrainInstallationDialog
#define HDR_laySaltGrainInstallationDialog
#include "laySalt.h"
#include <QDialog>
#include "ui_SaltGrainInstallationDialog.h"
namespace lay
{
class Salt;
/**
* @brief The dialog for managing the Salt ("Packages")
*/
class SaltGrainInstallationDialog
: public QDialog, private Ui::SaltGrainInstallationDialog
{
Q_OBJECT
public:
/**
* @brief Constructor
*/
SaltGrainInstallationDialog (QWidget *parent, lay::Salt *salt);
private slots:
/**
* @brief Called when the currently selected package (grain) has changed
*/
void current_changed ();
/**
* @brief Called when the Apply button is clicked
*/
void apply ();
/**
* @brief Called when the Mark button is pressed
*/
void mark ();
private:
lay::Salt *mp_salt;
lay::Salt m_salt_mine;
lay::SaltGrain *current_grain ();
};
}
#endif

View File

@ -233,4 +233,32 @@ SaltGrains::from_path (const std::string &path, const std::string &prefix)
return grains;
}
static tl::XMLElementList s_group_struct =
tl::make_member (&SaltGrains::name, &SaltGrains::set_name, "name") +
tl::make_element (&SaltGrains::begin_collections, &SaltGrains::end_collections, &SaltGrains::add_collection, "group", &s_group_struct) +
tl::make_element (&SaltGrains::begin_grains, &SaltGrains::end_grains, &SaltGrains::add_grain, "salt-grain", SaltGrain::xml_struct ());
static tl::XMLStruct<lay::SaltGrains> s_xml_struct ("salt-mine", s_group_struct);
void
SaltGrains::load (const std::string &p)
{
tl::XMLFileSource source (p);
s_xml_struct.parse (source, *this);
}
void
SaltGrains::load (tl::InputStream &p)
{
tl::XMLStreamSource source (p);
s_xml_struct.parse (source, *this);
}
void
SaltGrains::save (const std::string &p) const
{
tl::OutputStream os (p, tl::OutputStream::OM_Plain);
s_xml_struct.write (os, *this);
}
}

View File

@ -174,6 +174,21 @@ public:
*/
bool is_readonly () const;
/**
* @brief Loads the grain collection from the given path
*/
void load (const std::string &p);
/**
* @brief Loads the grain collection from the given input stream
*/
void load (tl::InputStream &p);
/**
* @brief Saves the grain collection to the given file
*/
void save (const std::string &p) const;
/**
* @brief Scan grains from a given path
* This will scan the grains found within this path and return a collection containing

View File

@ -21,209 +21,29 @@
*/
#include "laySaltManagerDialog.h"
#include "laySaltModel.h"
#include "laySaltGrainPropertiesDialog.h"
#include "laySaltGrainInstallationDialog.h"
#include "laySalt.h"
#include "ui_SaltGrainTemplateSelectionDialog.h"
#include "tlString.h"
#include "tlExceptions.h"
#include <QAbstractItemModel>
#include <QAbstractTextDocumentLayout>
#include <QStyledItemDelegate>
#include <QTextDocument>
#include <QPainter>
#include <QDir>
#include <QTextStream>
#include <QBuffer>
#include <QResource>
#include <QMessageBox>
#include <QAbstractItemModel>
#include <QStyledItemDelegate>
namespace lay
{
// --------------------------------------------------------------------------------------
/**
* @brief A model representing the salt grains for a QListView
*/
class SaltModel
: public QAbstractItemModel
{
public:
SaltModel (QObject *parent, lay::Salt *salt)
: QAbstractItemModel (parent), mp_salt (salt)
{
// .. nothing yet ..
}
QVariant data (const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
const lay::SaltGrain *g = mp_salt->begin_flat ()[index.row ()];
std::string text = "<html><body>";
text += "<h4>";
text += tl::escaped_to_html (g->name ());
if (!g->version ().empty ()) {
text += " ";
text += tl::escaped_to_html (g->version ());
}
if (!g->title ().empty ()) {
text += " - ";
text += tl::escaped_to_html (g->title ());
}
text += "</h4>";
if (!g->doc ().empty ()) {
text += "<p>";
text += tl::escaped_to_html (g->doc ());
text += "</p>";
}
text += "</body></html>";
return tl::to_qstring (text);
} else if (role == Qt::DecorationRole) {
int icon_dim = 64;
const lay::SaltGrain *g = mp_salt->begin_flat ()[index.row ()];
if (g->icon ().isNull ()) {
return QIcon (":/salt_icon.png");
} else {
QImage img = g->icon ();
if (img.width () == icon_dim && img.height () == icon_dim) {
return QPixmap::fromImage (img);
} else {
img = img.scaled (QSize (icon_dim, icon_dim), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QImage final_img (icon_dim, icon_dim, QImage::Format_ARGB32);
final_img.fill (QColor (0, 0, 0, 0));
QPainter painter (&final_img);
painter.drawImage ((icon_dim - img.width ()) / 2, (icon_dim - img.height ()) / 2, img);
return QPixmap::fromImage (final_img);
}
}
} else {
return QVariant ();
}
}
QModelIndex index (int row, int column, const QModelIndex &parent) const
{
if (parent.isValid ()) {
return QModelIndex ();
} else {
return createIndex (row, column, mp_salt->begin_flat () [row]);
}
}
QModelIndex parent (const QModelIndex & /*index*/) const
{
return QModelIndex ();
}
int columnCount(const QModelIndex & /*parent*/) const
{
return 1;
}
int rowCount (const QModelIndex &parent) const
{
if (parent.isValid ()) {
return 0;
} else {
return mp_salt->end_flat () - mp_salt->begin_flat ();
}
}
SaltGrain *grain_from_index (const QModelIndex &index) const
{
if (index.isValid ()) {
return static_cast<SaltGrain *> (index.internalPointer ());
} else {
return 0;
}
}
void update ()
{
reset ();
}
public:
lay::Salt *mp_salt;
};
// --------------------------------------------------------------------------------------
/**
* @brief A delegate displaying the summary of a grain
*/
class SaltItemDelegate
: public QStyledItemDelegate
{
public:
SaltItemDelegate (QObject *parent)
: QStyledItemDelegate (parent)
{
// .. nothing yet ..
}
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 optionV4 = option;
initStyleOption (&optionV4, index);
QStyle *style = optionV4.widget ? optionV4.widget->style () : QApplication::style ();
QTextDocument doc;
doc.setHtml (optionV4.text);
optionV4.text = QString ();
style->drawControl (QStyle::CE_ItemViewItem, &optionV4, painter);
QAbstractTextDocumentLayout::PaintContext ctx;
if (optionV4.state & QStyle::State_Selected) {
ctx.palette.setColor (QPalette::Text, optionV4.palette.color (QPalette::Active, QPalette::HighlightedText));
}
QRect textRect = style->subElementRect (QStyle::SE_ItemViewItemText, &optionV4);
painter->save ();
painter->translate (textRect.topLeft ());
painter->setClipRect (textRect.translated (-textRect.topLeft ()));
doc.documentLayout()->draw (painter, ctx);
painter->restore ();
}
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const
{
const int textWidth = 500;
QStyleOptionViewItemV4 optionV4 = option;
initStyleOption (&optionV4, index);
const QListView *view = dynamic_cast<const QListView *> (optionV4.widget);
QSize icon_size (0, 0);
if (view) {
icon_size = view->iconSize ();
}
QTextDocument doc;
doc.setHtml (optionV4.text);
doc.setTextWidth (textWidth);
return QSize (textWidth + icon_size.width () + 6, std::max (icon_size.height () + 12, int (doc.size ().height ())));
}
};
// --------------------------------------------------------------------------------------
/**
* @brief A tiny dialog to select a template and a name for the grain
*/
@ -384,17 +204,30 @@ END_PROTECTED
void
SaltManagerDialog::delete_grain ()
{
BEGIN_PROTECTED
// @@@
SaltGrain *g = current_grain ();
if (! g) {
throw tl::Exception (tl::to_string (tr ("No package selected to delete")));
}
if (QMessageBox::question (this, tr ("Delete Package"), tr ("Are you sure to delete package '%1'?").arg (tl::to_qstring (g->name ())), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
mp_salt->remove_grain (*g);
}
END_PROTECTED
}
void
SaltManagerDialog::install_grain ()
{
BEGIN_PROTECTED
// @@@
// @@@ TODO: cache this somewhere - don't recreate this dialog always
SaltGrainInstallationDialog inst_dialog (this, mp_salt);
inst_dialog.exec ();
END_PROTECTED
}
void
@ -405,6 +238,8 @@ SaltManagerDialog::salt_changed ()
return;
}
// NOTE: the disabling of the event handler prevents us from
// letting the model connect to the salt's signal directly.
m_current_changed_enabled = false;
model->update ();
m_current_changed_enabled = true;

207
src/lay/laySaltModel.cc Normal file
View File

@ -0,0 +1,207 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2017 Matthias Koefferlein
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "laySaltModel.h"
#include "laySalt.h"
#include <QIcon>
#include <QPainter>
#include <QAbstractTextDocumentLayout>
#include <QApplication>
#include <QListView>
namespace lay
{
// --------------------------------------------------------------------------------------
SaltItemDelegate::SaltItemDelegate (QObject *parent)
: QStyledItemDelegate (parent)
{
// .. nothing yet ..
}
void
SaltItemDelegate::paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 optionV4 = option;
initStyleOption (&optionV4, index);
QStyle *style = optionV4.widget ? optionV4.widget->style () : QApplication::style ();
QTextDocument doc;
doc.setHtml (optionV4.text);
optionV4.text = QString ();
style->drawControl (QStyle::CE_ItemViewItem, &optionV4, painter);
QAbstractTextDocumentLayout::PaintContext ctx;
if (optionV4.state & QStyle::State_Selected) {
ctx.palette.setColor (QPalette::Text, optionV4.palette.color (QPalette::Active, QPalette::HighlightedText));
}
QRect textRect = style->subElementRect (QStyle::SE_ItemViewItemText, &optionV4);
painter->save ();
painter->translate (textRect.topLeft ());
painter->setClipRect (textRect.translated (-textRect.topLeft ()));
doc.documentLayout()->draw (painter, ctx);
painter->restore ();
}
QSize
SaltItemDelegate::sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const
{
const int textWidth = 500;
QStyleOptionViewItemV4 optionV4 = option;
initStyleOption (&optionV4, index);
const QListView *view = dynamic_cast<const QListView *> (optionV4.widget);
QSize icon_size (0, 0);
if (view) {
icon_size = view->iconSize ();
}
QTextDocument doc;
doc.setHtml (optionV4.text);
doc.setTextWidth (textWidth);
return QSize (textWidth + icon_size.width () + 6, std::max (icon_size.height () + 12, int (doc.size ().height ())));
}
// --------------------------------------------------------------------------------------
SaltModel::SaltModel (QObject *parent, lay::Salt *salt)
: QAbstractItemModel (parent), mp_salt (salt)
{
// .. nothing yet ..
}
QVariant
SaltModel::data (const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
const lay::SaltGrain *g = mp_salt->begin_flat ()[index.row ()];
std::string text = "<html><body>";
text += "<h4>";
text += tl::escaped_to_html (g->name ());
if (!g->version ().empty ()) {
text += " ";
text += tl::escaped_to_html (g->version ());
}
if (!g->title ().empty ()) {
text += " - ";
text += tl::escaped_to_html (g->title ());
}
text += "</h4>";
if (!g->doc ().empty ()) {
text += "<p>";
text += tl::escaped_to_html (g->doc ());
text += "</p>";
}
text += "</body></html>";
return tl::to_qstring (text);
} else if (role == Qt::DecorationRole) {
int icon_dim = 64;
const lay::SaltGrain *g = mp_salt->begin_flat ()[index.row ()];
if (g->icon ().isNull ()) {
return QIcon (":/salt_icon.png");
} else {
QImage img = g->icon ();
if (img.width () == icon_dim && img.height () == icon_dim) {
return QPixmap::fromImage (img);
} else {
img = img.scaled (QSize (icon_dim, icon_dim), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QImage final_img (icon_dim, icon_dim, QImage::Format_ARGB32);
final_img.fill (QColor (0, 0, 0, 0));
QPainter painter (&final_img);
painter.drawImage ((icon_dim - img.width ()) / 2, (icon_dim - img.height ()) / 2, img);
return QPixmap::fromImage (final_img);
}
}
} else {
return QVariant ();
}
}
QModelIndex
SaltModel::index (int row, int column, const QModelIndex &parent) const
{
if (parent.isValid ()) {
return QModelIndex ();
} else {
return createIndex (row, column, mp_salt->begin_flat () [row]);
}
}
QModelIndex
SaltModel::parent (const QModelIndex & /*index*/) const
{
return QModelIndex ();
}
int
SaltModel::columnCount(const QModelIndex & /*parent*/) const
{
return 1;
}
int
SaltModel::rowCount (const QModelIndex &parent) const
{
if (parent.isValid ()) {
return 0;
} else {
return mp_salt->end_flat () - mp_salt->begin_flat ();
}
}
SaltGrain *
SaltModel::grain_from_index (const QModelIndex &index) const
{
if (index.isValid ()) {
return static_cast<SaltGrain *> (index.internalPointer ());
} else {
return 0;
}
}
void
SaltModel::update ()
{
reset ();
}
}

110
src/lay/laySaltModel.h Normal file
View File

@ -0,0 +1,110 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2017 Matthias Koefferlein
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef HDR_laySaltModel
#define HDR_laySaltModel
#include "layCommon.h"
#include <QObject>
#include <QAbstractItemModel>
#include <QStyledItemDelegate>
#include <string>
namespace lay
{
class Salt;
class SaltGrain;
/**
* @brief A model representing the salt grains for a QListView
*/
class SaltModel
: public QAbstractItemModel
{
Q_OBJECT
public:
/**
* @brief Constructor
*/
SaltModel (QObject *parent, lay::Salt *salt);
/**
* @brief Implementation of the QAbstractItemModel interface
*/
QVariant data (const QModelIndex &index, int role) const;
/**
* @brief Implementation of the QAbstractItemModel interface
*/
QModelIndex index (int row, int column, const QModelIndex &parent) const;
/**
* @brief Implementation of the QAbstractItemModel interface
*/
QModelIndex parent (const QModelIndex & /*index*/) const;
/**
* @brief Implementation of the QAbstractItemModel interface
*/
int columnCount(const QModelIndex & /*parent*/) const;
/**
* @brief Implementation of the QAbstractItemModel interface
*/
int rowCount (const QModelIndex &parent) const;
/**
* @brief Gets the grain pointer from a model index
*/
SaltGrain *grain_from_index (const QModelIndex &index) const;
/**
* @brief Updates the model
* Needs to be called when the salt has changed.
*/
void update ();
public:
lay::Salt *mp_salt;
};
// --------------------------------------------------------------------------------------
/**
* @brief A delegate displaying the summary of a grain
*/
class SaltItemDelegate
: public QStyledItemDelegate
{
public:
SaltItemDelegate (QObject *parent);
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
}
#endif

View File

@ -240,6 +240,15 @@ TEST (3)
EXPECT_EQ (grains_to_string (gg), "[a,b,c[c/u,c/c[c/c/v]]]");
EXPECT_EQ (gg.begin_collections ()->path (), tl::to_string (dir_c.absolutePath ()));
std::string gg_path = tmp_file ("gg.tmp");
gg.save (gg_path);
lay::SaltGrains ggg;
ggg.load (gg_path);
EXPECT_EQ (grains_to_string (ggg), "[a,b,c[c/u,c/c[c/c/v]]]");
// NOTE: The path is not set, so this will fail:
// EXPECT_EQ (gg == ggg, true);
gg.remove_grain (gg.begin_grains (), false);
EXPECT_EQ (grains_to_string (gg), "[b,c[c/u,c/c[c/c/v]]]");