18#include "keximacroproperty.h"
21#include <tqlineedit.h>
26#include <kpushbutton.h>
28#include <kiconloader.h>
31#include "../lib/variable.h"
32#include "../lib/macroitem.h"
34#define KEXIMACRO_PROPERTYEDITORTYPE 5682
44class KexiMacroProperty::Private
50 TDESharedPtr<KoMacro::MacroItem> macroitem;
57 : KoProperty::CustomProperty(parent)
60 d->macroitem = macroitem;
70void KexiMacroProperty::init()
72 Q_ASSERT( d->macroitem != 0 );
75 TDESharedPtr<KoMacro::Action> action = d->macroitem->action();
76 TDESharedPtr<KoMacro::Variable> actionvariable = action->variable(d->name);
77 if(! actionvariable.data()) {
78 kdDebug() <<
"KexiMacroProperty::createProperty() Skipped cause there exists no such action=" << d->name << endl;
82 TDESharedPtr<KoMacro::Variable>
variable = d->macroitem->variable(d->name,
true);
84 kdDebug() <<
"KexiMacroProperty::createProperty() Skipped cause there exists no such variable=" << d->name << endl;
91 Q_ASSERT(! d->name.isNull());
92 m_property->setName( d->name.latin1() );
93 m_property->setCaption( actionvariable->text() );
94 m_property->setDescription( action->comment() );
95 m_property->setValue(
variable->variant(),
true );
96 m_property->setType( KEXIMACRO_PROPERTYEDITORTYPE );
106 Q_UNUSED(rememberOldValue);
107 kdDebug()<<
"KexiMacroProperty::setValue name="<<d->name<<
" value="<<
value<<
" rememberOldValue="<<rememberOldValue<<endl;
108 if(! d->macroitem->setVariant(d->name,
value)) {
109 kdDebug()<<
"KexiMacroProperty::setValue Update failed !!!"<<endl;
124 TDESharedPtr<KoMacro::Variable>
variable = d->macroitem->variable(d->name,
true);
146 return d->macroitem->variable(d->name,
true);
151 KoProperty::Property*
property =
new KoProperty::Property();
153 if(! customproperty->
variable().data()) {
154 kdWarning() <<
"KexiMacroProperty::createProperty() No such variable" << endl;
155 delete customproperty; customproperty = 0;
156 delete property;
property = 0;
159 property->setCustomProperty(customproperty);
168 : KoProperty::CustomPropertyFactory(parent)
178 kdDebug()<<
"KexiMacroPropertyFactory::createCustomProperty parent="<<parent->name()<<endl;
180 KoProperty::CustomProperty* customproperty = parent->customProperty();
182 if(! parentcustomproperty) {
183 kdWarning() <<
"KexiMacroPropertyFactory::createCustomProperty() parent=" << parent->name() <<
" has an invalid customproperty." << endl;
187 TDESharedPtr<KoMacro::MacroItem> macroitem = parentcustomproperty->
macroItem();
188 Q_ASSERT( macroitem.data() != 0 );
189 const TQString name = parentcustomproperty->
name();
190 Q_ASSERT(! name.isEmpty());
193 if(! macroproperty->
variable().data()) {
194 delete macroproperty; macroproperty = 0;
198 return macroproperty;
203 kdDebug()<<
"KexiMacroPropertyFactory::createCustomWidget property="<<
property->name()<<endl;
209 CustomPropertyFactory* factory = KoProperty::FactoryManager::self()->factoryForEditorType(KEXIMACRO_PROPERTYEDITORTYPE);
212 KoProperty::FactoryManager::self()->registerFactoryForEditor(KEXIMACRO_PROPERTYEDITORTYPE, factory);
225class ListBoxItem :
public TQListBoxText
228 ListBoxItem(TQListBox* listbox)
229 : TQListBoxText(listbox), m_enabled(true) {}
230 ListBoxItem(TQListBox* listbox,
const TQString& text, TQListBoxItem* after)
231 : TQListBoxText(listbox, text, after), m_enabled(true) {}
232 virtual ~ListBoxItem() {}
233 void setEnabled(
bool enabled) { m_enabled = enabled; }
234 virtual int width(
const TQListBox* lb)
const {
235 Q_ASSERT(
dynamic_cast<KComboBox*
>( lb->parent() ) );
236 return static_cast<KComboBox*
>( lb->parent() )->lineEdit()->width() + 2;
238 virtual int height(
const TQListBox* lb)
const {
239 Q_ASSERT(
dynamic_cast<KComboBox*
>( lb->parent() ) );
240 return m_enabled ?
static_cast<KComboBox*
>( lb->parent() )->height() + 2 : 0;
250class EditListBoxItem :
public ListBoxItem
254 EditListBoxItem(TQListBox* listbox, KexiMacroProperty* macroproperty)
255 : ListBoxItem(listbox)
256 , m_macroproperty(macroproperty)
263 virtual ~EditListBoxItem() {
268 virtual TQString text()
const {
269 TDESharedPtr<KoMacro::Variable> variable = m_macroproperty->variable();
270 Q_ASSERT( variable.data() );
272 Q_ASSERT( variable->toString() != TQString() );
273 return variable->toString();
276 KoProperty::Widget* widget()
const {
return m_widget; }
277 TDESharedPtr<KoMacro::MacroItem> macroItem()
const {
return m_macroproperty->macroItem(); }
278 TDESharedPtr<KoMacro::Variable> variable()
const {
return m_macroproperty->variable(); }
279 TDESharedPtr<KoMacro::Action> action()
const {
return m_macroproperty->macroItem()->action(); }
282 virtual void paint(TQPainter* p) {
283 if(! m_widget)
return;
284 Q_ASSERT(
dynamic_cast<KComboBox*
>( listBox()->parent() ) );
285 const int w = width(listBox());
286 const int h = height(listBox());
287 m_widget->setFixedSize(w - 2, h - 2);
288 p->drawPixmap(0, 0, TQPixmap::grabWidget(m_widget), 1, 1, w - 1, h - 1);
293 TDESharedPtr<KoMacro::MacroItem> macroitem = m_macroproperty->macroItem();
294 Q_ASSERT( macroitem.data() );
295 TDESharedPtr<KoMacro::Action> action = m_macroproperty->macroItem()->action();
296 if(! action.data()) {
297 kdWarning() <<
"EditListBoxItem::EditListBoxItem() Skipped cause there exists no action for macroproperty=" << m_macroproperty->name() << endl;
300 KoProperty::Property* parentproperty = m_macroproperty->parentProperty();
301 if(! parentproperty) {
302 kdWarning() <<
"EditListBoxItem::EditListBoxItem() No parentproperty defined" << endl;
305 TDESharedPtr<KoMacro::Variable> variable = m_macroproperty->variable();
306 if(! variable.data()) {
307 kdWarning() <<
"EditListBoxItem::EditListBoxItem() No variable defined for property=" << parentproperty->name() << endl;
311 TQVariant variant = variable->variant();
313 TDESharedPtr<KoMacro::Variable> actionvariable = action->variable(m_macroproperty->name());
314 if(actionvariable.data()) {
315 TQVariant actionvariant = actionvariable->variant();
316 Q_ASSERT( ! actionvariant.isNull() );
317 Q_ASSERT( variant.canCast(actionvariant.type()) );
318 variant.cast( actionvariant.type() );
321 int type = KoProperty::Auto;
322 switch(variant.type()) {
323 case TQVariant::UInt:
324 case TQVariant::Int: {
325 type = KoProperty::Integer;
327 case TQVariant::CString:
328 case TQVariant::String: {
329 type = KoProperty::String;
332 kdWarning() <<
"EditListBoxItem::EditListBoxItem() name=" << variable->name() <<
" type=" << TQVariant::typeToName(variant.type()) << endl;
336 TQString name = variable->name();
337 Q_ASSERT(! name.isNull());
339 m_prop =
new KoProperty::Property(
348 m_widget = KoProperty::FactoryManager::self()->createWidgetForProperty(m_prop);
349 Q_ASSERT( m_widget != 0 );
351 m_widget->reparent(listBox(), 0, TQPoint(1,1));
353 m_widget->setMinimumHeight(5);
354 m_widget->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
358 KexiMacroProperty* m_macroproperty;
359 KoProperty::Property* m_prop;
360 KoProperty::Widget* m_widget;
367class ListBox :
public TQListBox
370 ListBox(KComboBox* parent, KexiMacroProperty* macroproperty)
372 , m_macroproperty(macroproperty)
375 viewport()->setBackgroundMode(PaletteBackground);
376 setVariableHeight(
true);
380 virtual ~ListBox() {}
388 m_edititem =
new EditListBoxItem(
this, m_macroproperty);
389 Q_ASSERT( m_edititem->widget() != 0 );
391 const TQString name = m_macroproperty->name();
396 TDESharedPtr<KoMacro::Variable> itemvar = m_macroproperty->macroItem()->variable(name,
false);
399 actionchildren = itemvar->children();
401 TDESharedPtr<KoMacro::Action> action = m_edititem->action();
402 TDESharedPtr<KoMacro::Variable> actionvar = action.data() ? action->variable(name) : TDESharedPtr<KoMacro::Variable>();
405 actionchildren += actionvar->children();
407 KoMacro::Variable::List::ConstIterator it(actionchildren.constBegin()), end(actionchildren.constEnd());
408 for(; it != end; ++it) {
409 if(name == (*it)->name()) {
411 KoMacro::Variable::List::ConstIterator listit(list.constBegin()), listend(list.constEnd());
412 for(; listit != listend; ++listit)
413 children.append( *listit );
417 if(children.count() <= 0)
418 children = actionchildren;
429 if(children.count() > 0) {
430 KoMacro::Variable::List::Iterator childit(children.begin()), childend(children.end());
431 for(; childit != childend; ++childit) {
432 const TQString n = (*childit)->name();
434 const TQVariant v = (*childit)->variant();
443 case TQVariant::List: {
444 const TQValueList<TQVariant> list = v.toList();
445 for(TQValueList<TQVariant>::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it) {
446 const TQString s = (*it).toString().stripWhiteSpace();
451 case TQVariant::StringList: {
452 const TQStringList list = v.toStringList();
453 for(TQStringList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it)
454 if(! (*it).isEmpty())
458 const TQString s = v.toString().stripWhiteSpace();
466 TQListBoxItem* item = m_edititem;
467 const uint count = m_items.count();
468 for(uint i = 0; i < count; i++)
469 item =
new ListBoxItem(
this, m_items[i], item);
472 EditListBoxItem* editItem()
const {
return m_edititem; }
473 TQStringList items()
const {
return m_items; }
475 virtual void hide () {
477 for(uint i = 0; i < count(); i++)
478 static_cast<ListBoxItem*
>( item(i) )->setEnabled(
false);
480 virtual void show() {
487 KexiMacroProperty* m_macroproperty;
488 EditListBoxItem* m_edititem;
489 TQStringList m_items;
496class KexiMacroPropertyWidget::Private
499 KexiMacroProperty* macroproperty;
505 : KoProperty::Widget(property, parent)
508 kdDebug() <<
"KexiMacroPropertyWidget::KexiMacroPropertyWidget() Ctor" << endl;
510 TQHBoxLayout* layout =
new TQHBoxLayout(
this, 0, 0);
512 d->macroproperty =
dynamic_cast<KexiMacroProperty*
>(
property->customProperty() );
513 if(! d->macroproperty) {
514 kdWarning() <<
"KexiMacroPropertyWidget::KexiMacroPropertyWidget() Missing macroproperty for property=" <<
property->name() << endl;
518 d->combobox =
new KComboBox(
this);
519 layout->addWidget(d->combobox);
520 d->listbox =
new ListBox(d->combobox, d->macroproperty);
521 d->combobox->setEditable(
true);
522 d->combobox->setListBox(d->listbox);
523 d->combobox->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
524 d->combobox->setMinimumHeight(5);
525 d->combobox->setInsertionPolicy(TQComboBox::NoInsertion);
526 d->combobox->setMinimumSize(10, 0);
527 d->combobox->setAutoCompletion(
false);
528 d->combobox->setContextMenuEnabled(
false);
530 TQVariant
value = d->macroproperty->value();
531 int index = d->listbox->items().findIndex(
value.toString() );
533 d->combobox->setCurrentItem(index + 1);
534 d->listbox->setCurrentItem(index + 1);
537 Q_ASSERT( d->listbox->editItem()->widget() != 0 );
538 d->listbox->editItem()->widget()->setValue( d->macroproperty->value(),
true );
541 kdDebug() <<
">>> KexiMacroPropertyWidget::KexiMacroPropertyWidget() CurrentItem=" << d->combobox->currentItem() << endl;
543 d->combobox->setFocusProxy( d->listbox->editItem()->widget() );
544 setFocusWidget( d->combobox->lineEdit() );
546 connect(d->combobox, TQ_SIGNAL(textChanged(
const TQString&)),
547 this, TQ_SLOT(slotComboBoxChanged()));
548 connect(d->combobox, TQ_SIGNAL(activated(
int)),
549 this, TQ_SLOT(slotComboBoxActivated()));
550 connect(d->listbox->editItem()->widget(), TQ_SIGNAL(valueChanged(Widget*)),
551 this, TQ_SLOT(slotWidgetValueChanged()));
552 connect(d->macroproperty, TQ_SIGNAL(valueChanged()),
553 this, TQ_SLOT(slotPropertyValueChanged()));
558 kdDebug() <<
"KexiMacroPropertyWidget::~KexiMacroPropertyWidget() Dtor" << endl;
564 kdDebug()<<
"KexiMacroPropertyWidget::value() value="<<d->macroproperty->value()<<endl;
565 return d->macroproperty->value();
573 kdDebug()<<
"KexiMacroPropertyWidget::setValue() value="<<
value<<
" emitChange="<<emitChange<<endl;
576 d->combobox->blockSignals(
true);
578 const TQString s =
value.toString();
579 d->combobox->setCurrentText( s.isNull() ?
"" : s );
582 emit valueChanged(
this);
584 d->combobox->blockSignals(
false);
593void KexiMacroPropertyWidget::slotComboBoxChanged()
595 kdDebug()<<
"KexiMacroPropertyWidget::slotComboBoxChanged()"<<endl;
596 const TQVariant v = d->combobox->currentText();
597 d->macroproperty->
setValue(v,
true);
601void KexiMacroPropertyWidget::slotComboBoxActivated()
603 Q_ASSERT( d->listbox->editItem()->widget() );
604 const int index = d->combobox->currentItem();
605 TQString text = (index == 0)
606 ? d->listbox->editItem()->widget()->value().toString()
607 : d->combobox->text(index);
608 kdDebug()<<
"KexiMacroPropertyWidget::slotComboBoxActivated() index="<<index<<
" text="<<text<<endl;
609 d->combobox->setCurrentText(text);
610 slotWidgetValueChanged();
613void KexiMacroPropertyWidget::slotWidgetValueChanged()
615 d->macroproperty->emitPropertyChanged();
618void KexiMacroPropertyWidget::slotPropertyValueChanged()
620 Q_ASSERT( d->listbox->editItem()->widget() );
621 const TQVariant v = d->macroproperty->value();
622 kdDebug()<<
"KexiMacroPropertyWidget::slotPropertyValueChanged() value="<<v<<endl;
623 d->listbox->editItem()->widget()->setValue(v,
true);
626#include "keximacroproperty.moc"
virtual KoProperty::CustomProperty * createCustomProperty(KoProperty::Property *parent)
virtual KoProperty::Widget * createCustomWidget(KoProperty::Property *property)
KexiMacroPropertyFactory(TQObject *parent)
Constructor.
static void initFactory()
Initializes this factory.
virtual ~KexiMacroPropertyFactory()
Destructor.
Implementation of a KoProperty::CustomProperty to have more control about the handling of our macro-p...
KoProperty::Property * parentProperty() const
TDESharedPtr< KoMacro::MacroItem > macroItem() const
static KoProperty::Property * createProperty(TDESharedPtr< KoMacro::MacroItem > macroitem, const TQString &name)
Factory function to create a new KoProperty::Property instance that will use a KexiMacroProperty as c...
virtual ~KexiMacroProperty()
Destructor.
KexiMacroProperty(KoProperty::Property *parent, TDESharedPtr< KoMacro::MacroItem > macroitem, const TQString &name)
Constructor.
virtual bool handleValue() const
Tells whether CustomProperty should be used to get the property's value.
TDESharedPtr< KoMacro::Variable > variable() const
virtual void setValue(const TQVariant &value, bool rememberOldValue)
This function is called by KoProperty::Property::setValue() when a custom property is set.
void valueChanged()
Emitted if setValue was called and the value changed.
virtual TQVariant value() const
This function is called by KoProperty::Property::value() when a custom property is set and handleValu...
TQValueList< TDESharedPtr< Variable > > List
Property to get and set a TQVariant as variable.