WIP: refactoring editor options - moved some classes from edt to lay namespace

This commit is contained in:
Matthias Koefferlein
2020-09-06 18:45:58 +02:00
parent fb90144176
commit 46b5b87eaf
21 changed files with 491 additions and 416 deletions
@@ -0,0 +1,65 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2020 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 "tlInternational.h"
#include "layEditorOptionsPage.h"
#include "layEditorOptionsPages.h"
namespace lay
{
// ------------------------------------------------------------------
// EditorOptionsPage implementation
EditorOptionsPage::EditorOptionsPage (lay::Dispatcher *dispatcher)
: QWidget (0), mp_owner (0), m_active (true), mp_plugin_declaration (0), mp_dispatcher (dispatcher)
{
// nothing yet ..
}
EditorOptionsPage::~EditorOptionsPage ()
{
set_owner (0);
}
void
EditorOptionsPage::set_owner (EditorOptionsPages *owner)
{
if (mp_owner) {
mp_owner->unregister_page (this);
}
mp_owner = owner;
}
void
EditorOptionsPage::activate (bool active)
{
if (m_active != active) {
m_active = active;
if (mp_owner) {
mp_owner->activate_page (this);
}
}
}
}
@@ -0,0 +1,82 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2020 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_layEditorOptionsPage
#define HDR_layEditorOptionsPage
#include <QWidget>
namespace lay
{
class PluginDeclaration;
class Dispatcher;
class Plugin;
class EditorOptionsPages;
/**
* @brief The base class for a object properties page
*/
class EditorOptionsPage
: public QWidget
{
Q_OBJECT
public:
EditorOptionsPage (lay::Dispatcher *dispatcher);
virtual ~EditorOptionsPage ();
virtual std::string title () const = 0;
virtual int order () const = 0;
virtual void apply (lay::Dispatcher * /*root*/) { }
virtual void setup (lay::Dispatcher * /*root*/) { }
virtual void commit_recent (lay::Dispatcher * /*root*/) { }
bool active () const { return m_active; }
void activate (bool active);
void set_owner (EditorOptionsPages *owner);
const lay::PluginDeclaration *plugin_declaration () const { return mp_plugin_declaration; }
void set_plugin_declaration (const lay::PluginDeclaration *pd) { mp_plugin_declaration = pd; }
protected slots:
void edited ()
{
apply (dispatcher ());
}
protected:
lay::Dispatcher *dispatcher () const
{
return mp_dispatcher;
}
private:
EditorOptionsPages *mp_owner;
bool m_active;
const lay::PluginDeclaration *mp_plugin_declaration;
lay::Dispatcher *mp_dispatcher;
};
}
#endif
@@ -0,0 +1,204 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2020 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 "tlInternational.h"
#include "layEditorOptionsPages.h"
#include "tlExceptions.h"
#include "layPlugin.h"
#include "layLayoutView.h"
#include "layQtTools.h"
#include "ui_EditorOptionsGeneric.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTabWidget>
#include <QToolButton>
#include <QCompleter>
namespace lay
{
// ------------------------------------------------------------------
// EditorOptionsPages implementation
struct EOPCompareOp
{
bool operator() (lay::EditorOptionsPage *a, lay::EditorOptionsPage *b) const
{
return a->order () < b->order ();
}
};
EditorOptionsPages::EditorOptionsPages (QWidget *parent, const std::vector<lay::EditorOptionsPage *> &pages, lay::Dispatcher *dispatcher)
: QFrame (parent), mp_dispatcher (dispatcher)
{
QVBoxLayout *ly1 = new QVBoxLayout (this);
ly1->setMargin (0);
mp_pages = new QTabWidget (this);
ly1->addWidget (mp_pages);
m_pages = pages;
for (std::vector <lay::EditorOptionsPage *>::const_iterator p = m_pages.begin (); p != m_pages.end (); ++p) {
(*p)->set_owner (this);
}
update (0);
setup ();
}
EditorOptionsPages::~EditorOptionsPages ()
{
while (m_pages.size () > 0) {
delete m_pages.front ();
}
}
void
EditorOptionsPages::focusInEvent (QFocusEvent * /*event*/)
{
// Sends the focus to the current page's last focus owner
if (mp_pages->currentWidget () && mp_pages->currentWidget ()->focusWidget ()) {
mp_pages->currentWidget ()->focusWidget ()->setFocus ();
}
}
void
EditorOptionsPages::unregister_page (lay::EditorOptionsPage *page)
{
std::vector <lay::EditorOptionsPage *> pages;
for (std::vector <lay::EditorOptionsPage *>::const_iterator p = m_pages.begin (); p != m_pages.end (); ++p) {
if (*p != page) {
pages.push_back (*p);
}
}
m_pages = pages;
update (0);
}
void
EditorOptionsPages::activate_page (lay::EditorOptionsPage *page)
{
try {
if (page->active ()) {
page->setup (mp_dispatcher);
}
} catch (...) {
// catch any errors related to configuration file errors etc.
}
update (page);
}
void
EditorOptionsPages::update (lay::EditorOptionsPage *page)
{
std::vector <lay::EditorOptionsPage *> sorted_pages = m_pages;
std::sort (sorted_pages.begin (), sorted_pages.end (), EOPCompareOp ());
if (! page && m_pages.size () > 0) {
page = m_pages.back ();
}
while (mp_pages->count () > 0) {
mp_pages->removeTab (0);
}
int index = -1;
for (std::vector <lay::EditorOptionsPage *>::iterator p = sorted_pages.begin (); p != sorted_pages.end (); ++p) {
if ((*p)->active ()) {
if ((*p) == page) {
index = mp_pages->count ();
}
mp_pages->addTab (*p, tl::to_qstring ((*p)->title ()));
} else {
(*p)->setParent (0);
}
}
if (index < 0) {
index = mp_pages->currentIndex ();
}
if (index >= int (mp_pages->count ())) {
index = mp_pages->count () - 1;
}
mp_pages->setCurrentIndex (index);
setVisible (mp_pages->count () > 0);
}
void
EditorOptionsPages::setup ()
{
try {
for (std::vector <lay::EditorOptionsPage *>::iterator p = m_pages.begin (); p != m_pages.end (); ++p) {
if ((*p)->active ()) {
(*p)->setup (mp_dispatcher);
}
}
// make the display consistent with the status (this is important for
// PCell parameters where the PCell may be asked to modify the parameters)
do_apply ();
} catch (...) {
// catch any errors related to configuration file errors etc.
}
}
void
EditorOptionsPages::do_apply ()
{
for (std::vector <lay::EditorOptionsPage *>::iterator p = m_pages.begin (); p != m_pages.end (); ++p) {
if ((*p)->active ()) {
// NOTE: we apply to the root dispatcher, so other dispatchers (views) get informed too.
(*p)->apply (mp_dispatcher->dispatcher ());
}
}
}
void
EditorOptionsPages::apply ()
{
BEGIN_PROTECTED
do_apply ();
END_PROTECTED_W (this)
}
// ------------------------------------------------------------------
// Indicates an error on a line edit
template <class Value>
static void configure_from_line_edit (lay::Dispatcher *dispatcher, QLineEdit *le, const std::string &cfg_name)
{
try {
Value value = Value (0);
tl::from_string (tl::to_string (le->text ()), value);
dispatcher->config_set (cfg_name, tl::to_string (value));
lay::indicate_error (le, 0);
} catch (tl::Exception &ex) {
lay::indicate_error (le, &ex);
}
}
}
@@ -0,0 +1,82 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2020 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_layEditorOptionsPages
#define HDR_layEditorOptionsPages
#include "layEditorOptionsPage.h"
#include <tlVariant.h>
#include <QFrame>
#include <vector>
#include <string>
class QTabWidget;
class QLabel;
namespace lay
{
class PluginDeclaration;
class Dispatcher;
class Plugin;
/**
* @brief The object properties dialog
*/
class EditorOptionsPages
: public QFrame
{
Q_OBJECT
public:
EditorOptionsPages (QWidget *parent, const std::vector<lay::EditorOptionsPage *> &pages, lay::Dispatcher *root);
~EditorOptionsPages ();
void unregister_page (lay::EditorOptionsPage *page);
void activate_page (lay::EditorOptionsPage *page);
void focusInEvent (QFocusEvent *event);
const std::vector <lay::EditorOptionsPage *> &pages () const
{
return m_pages;
}
public slots:
void apply ();
void setup ();
private:
std::vector <lay::EditorOptionsPage *> m_pages;
lay::Dispatcher *mp_dispatcher;
QTabWidget *mp_pages;
void update (lay::EditorOptionsPage *page);
void do_apply ();
};
}
#endif
+20
View File
@@ -31,6 +31,7 @@
#include <QSplitter>
#include <QWidget>
#include <QLabel>
#include <QPalette>
#include <stdio.h>
@@ -159,5 +160,24 @@ restore_dialog_state (QWidget *dialog, const std::string &s, bool with_section_s
}
}
void
indicate_error (QWidget *le, const tl::Exception *ex)
{
// by the way, update the foreground color of the cell edit box as well (red, if not valid)
QPalette pl = le->palette ();
if (ex) {
pl.setColor (QPalette::Active, QPalette::Text, Qt::red);
pl.setColor (QPalette::Active, QPalette::Base, QColor (Qt::red).lighter (180));
le->setToolTip (tl::to_qstring (ex->msg ()));
} else {
QWidget *pw = dynamic_cast<QWidget *> (le->parent ());
tl_assert (pw != 0);
pl.setColor (QPalette::Active, QPalette::Text, pw->palette ().color (QPalette::Text));
pl.setColor (QPalette::Active, QPalette::Base, pw->palette ().color (QPalette::Base));
le->setToolTip (QString ());
}
le->setPalette (pl);
}
}
+13
View File
@@ -32,6 +32,11 @@ class QLabel;
class QWidget;
class QObject;
namespace tl
{
class Exception;
}
namespace lay
{
@@ -62,6 +67,14 @@ LAYBASIC_PUBLIC void activate_modal_help_links (QLabel *label);
*/
LAYBASIC_PUBLIC void register_help_handler (QObject *object, const char *slot, const char *modal_slot);
/**
* @brief Configures a QLineEdit or other widget to indicate an error
*
* When a non-null ex pointer is passed, the background will be turned red
* and the exception's text will be used as tooltip. Use this function with
* a null ex pointer to clear the error condition.
*/
void indicate_error (QWidget *le, const tl::Exception *ex);
} // namespace lay
+4
View File
@@ -115,6 +115,8 @@ SOURCES = \
layEditable.cc \
layEditStipplesForm.cc \
layEditStippleWidget.cc \
layEditorOptionsPage.cc \
layEditorOptionsPages.cc \
layFileDialog.cc \
layFinder.cc \
layFixedFont.cc \
@@ -216,6 +218,8 @@ HEADERS = \
layEditable.h \
layEditStipplesForm.h \
layEditStippleWidget.h \
layEditorOptionsPage.h \
layEditorOptionsPages.h \
layFileDialog.h \
layFinder.h \
layFixedFont.h \