📄 ctreeview.cpp
字号:
/*************************************************************************** CTreeView.cpp The TreeView class (c) 2000-2003 Beno� Minisini <gambas@users.sourceforge.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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.***************************************************************************/#define __CTREEVIEW_CPP#include <qapplication.h>#include <qheader.h>#include <qpalette.h>#include <qscrollview.h>#include <qlistview.h>#if QT_VERSION >= 0x030200#include <qobjectlist.h>#else#include <qobjcoll.h>#endif#include "gambas.h"#include "CWidget.h"#include "CPicture.h"#include "CTreeView.h"DECLARE_EVENT(EVENT_Select); /* selection change */DECLARE_EVENT(EVENT_Click); /* simple click */DECLARE_EVENT(EVENT_Activate); /* double click */DECLARE_EVENT(EVENT_Rename); /* double click */DECLARE_EVENT(EVENT_Expand);DECLARE_EVENT(EVENT_Collapse);DECLARE_EVENT(EVENT_ColumnClick); /* simple click */// Ceci est un test !static int _column = -1;/*************************************************************************** class MyListViewItem***************************************************************************/void MyListViewItem::initData(CTREEVIEW *cont){ picture = 0; key = 0; last = 0; prev = 0; container = cont; //sortKey = NULL; setDropEnabled(true);}MyListViewItem::MyListViewItem(CTREEVIEW *cont, MyListView *parent): QListViewItem(parent, parent->last){ initData(cont); prev = parent->last; parent->last = this;}MyListViewItem::MyListViewItem(CTREEVIEW *cont, MyListView *parent, MyListViewItem *after): QListViewItem(parent, after){ initData(cont); prev = after; if (after == parent->last) parent->last = this; else { MyListViewItem *n = ((MyListViewItem *)nextSibling()); n->prev = this; }}MyListViewItem::MyListViewItem(CTREEVIEW *cont, MyListViewItem *parent): QListViewItem(parent, parent->last){ initData(cont); prev = parent->last; parent->last = this;}MyListViewItem::MyListViewItem(CTREEVIEW *cont, MyListViewItem *parent, MyListViewItem *after): QListViewItem(parent, after){ initData(cont); if (after == parent->last) parent->last = this; else { MyListViewItem *n = ((MyListViewItem *)nextSibling()); n->prev = this; }}MyListViewItem::~MyListViewItem(){ MyListView *lw = (MyListView *)container->widget.widget; MyListViewItem *par = (MyListViewItem *)parent(); //qDebug("< delete item %s %p", key, this); if (par) { if (par->last == this) par->last = prev; } else { if (lw->last == this) lw->last = prev; } par = ((MyListViewItem *)nextSibling()); if (par) par->prev = prev; //qDebug("container = %p ->item = %p", container, container->item); if (container->item == this) container->item = par; if (container->save == this) container->save = 0; //if (container->last == this) // container->last = NULL; container->dict->remove(key); GB.Unref((void **)&picture); GB.FreeString(&key); //qDebug("container = %p ->item = %p", container, container->item); //qDebug(">");}void MyListViewItem::setPicture(GB_OBJECT *pict){ SET_PIXMAP(setPixmap, &picture, pict);}int MyListViewItem::compare(QListViewItem *i, int col, bool ascending) const{ if ((listView()->columnAlignment(col) & AlignHorizontal_Mask) == AlignRight) { if (text(col).length() > i->text(col).length()) return 1; if (text(col).length() < i->text(col).length()) return -1; } return QListViewItem::compare(i, col, ascending);}/*************************************************************************** class MyListView***************************************************************************/MyListView::MyListView(QWidget *parent): QListView(parent){ last = 0;}/*************************************************************************** TreeView***************************************************************************/static void return_item(void *_object, MyListViewItem *item){ if (!item) THIS->save = THIS->item; THIS->item = item; GB.ReturnBoolean(item == 0);}static MyListView *listview_init(void *_object, void *parent){ MyListView *wid; wid = new MyListView(CONTAINER(parent)); QObject::connect(wid, SIGNAL(selectionChanged()), &CTreeView::manager, SLOT(selected())); QObject::connect(wid, SIGNAL(doubleClicked(QListViewItem *)), &CTreeView::manager, SLOT(activated(QListViewItem *))); QObject::connect(wid, SIGNAL(clicked(QListViewItem *)), &CTreeView::manager, SLOT(clicked(QListViewItem *))); QObject::connect(wid, SIGNAL(itemRenamed(QListViewItem *, int)), &CTreeView::manager, SLOT(renamed(QListViewItem *, int))); CWIDGET_new(wid, (void *)_object, "ListView"); THIS->dict = new QAsciiDict<MyListViewItem>; THIS->sorted = -1; THIS->asc = false; THIS->item = 0; THIS->save = 0; THIS->rename = false; wid->setSorting(-1); return wid;}BEGIN_METHOD(CTREEVIEW_new, GB_OBJECT parent) MyListView *wid = listview_init(_object, VARG(parent)); QObject::connect(wid, SIGNAL(expanded(QListViewItem *)), &CTreeView::manager, SLOT(expanded(QListViewItem *))); QObject::connect(wid, SIGNAL(collapsed(QListViewItem *)), &CTreeView::manager, SLOT(collapsed(QListViewItem *))); wid->addColumn("-"); wid->header()->hide(); wid->setRootIsDecorated(true); wid->show();END_METHODBEGIN_METHOD(CLISTVIEW_new, GB_OBJECT parent) MyListView *wid = listview_init(_object, VARG(parent)); wid->addColumn("-"); wid->setColumnWidthMode(0, QListView::Manual); wid->setAllColumnsShowFocus(true); wid->setHScrollBarMode(QScrollView::AlwaysOff); wid->header()->hide(); wid->setResizeMode(QListView::LastColumn); wid->show();END_METHODBEGIN_METHOD(CCOLUMNVIEW_new, GB_OBJECT parent) MyListView *wid = listview_init(_object, VARG(parent)); QObject::connect(wid, SIGNAL(clicked(QListViewItem *, const QPoint &, int)), &CTreeView::manager, SLOT(columnClicked(QListViewItem *, const QPoint &, int))); QObject::connect(wid->header(), SIGNAL(clicked(int)), &CTreeView::manager, SLOT(headerClicked(int))); QObject::connect(wid->header(), SIGNAL(sizeChange(int, int, int)), &CTreeView::manager, SLOT(headerSizeChange(int, int, int))); QObject::connect(wid, SIGNAL(expanded(QListViewItem *)), &CTreeView::manager, SLOT(expanded(QListViewItem *))); QObject::connect(wid, SIGNAL(collapsed(QListViewItem *)), &CTreeView::manager, SLOT(collapsed(QListViewItem *))); wid->addColumn(" "); wid->setColumnAlignment(0, Qt::AlignLeft); //wid->setColumnWidthMode(0, QListView::Manual); wid->header()->setMovingEnabled(false); wid->setAllColumnsShowFocus(true); wid->show();END_METHODBEGIN_METHOD_VOID(CTREEVIEW_free) delete THIS->dict; //if (THIS->numeric) // delete[] THIS->numeric;END_METHOD/*BEGIN_PROPERTY(CTREEVIEW_drop) if (READ_PROPERTY) GB.ReturnBoolean(WIDGET->acceptDrops()); else { WIDGET->setAcceptDrops(VPROP(GB_BOOLEAN)); WIDGET->viewport()->setAcceptDrops(VPROP(GB_BOOLEAN)); }END_PROPERTY*/BEGIN_METHOD_VOID(CTREEVIEW_clear) WIDGET->clear(); THIS->dict->clear();END_METHODBEGIN_METHOD(CTREEVIEW_add, GB_STRING key; GB_STRING text; GB_OBJECT picture; GB_STRING parent; GB_STRING after) MyListViewItem *item; MyListView *wid = WIDGET; char *key = GB.ToZeroString(ARG(key)); MyListViewItem *parent = NULL; MyListViewItem *after = NULL; char *akey; /* if (GB.IsMissing(4)) qDebug("CTREEVIEW_add: key = %s parent = NULL", key); else qDebug("CTREEVIEW_add: key = %s parent = %s", key, GB.ToZeroString(PARAM(parent))); */ if (*key == 0) { GB.Error("Null key"); return; } item = (*THIS->dict)[key]; if (item != NULL) { GB.Error("Key already used: &1", key); return; } if (!MISSING(parent)) { akey = GB.ToZeroString(ARG(parent)); parent = NULL; if (*akey) { parent = (*THIS->dict)[akey]; if (parent == NULL) { GB.Error("Parent item does not exist"); return; } } } if (!MISSING(after)) { akey = GB.ToZeroString(ARG(after)); if (*akey) { after = (*THIS->dict)[akey]; if (after == NULL) { GB.Error("After item does not exist"); return; } } } else after = NULL; if (parent == NULL) { if (after == NULL) item = new MyListViewItem(THIS, wid); else item = new MyListViewItem(THIS, wid, after); } else { if (after == NULL) item = new MyListViewItem(THIS, parent); else item = new MyListViewItem(THIS, parent, after); } item->setText(0, QSTRING_ARG(text)); GB.StoreString(ARG(key), &item->key); THIS->dict->insert(item->key, item); if (!MISSING(picture)) item->setPicture(ARG(picture)); item->setRenameEnabled(0, THIS->rename); THIS->item = item; RETURN_SELF();END_METHODBEGIN_METHOD(CLISTVIEW_add, GB_STRING key; GB_STRING text; GB_OBJECT picture; GB_STRING after) MyListViewItem *item; MyListView *wid = QLISTVIEW(_object); char *key = GB.ToZeroString(ARG(key)); MyListViewItem *after = NULL; char *akey; if (*key == 0) { GB.Error("Null key"); return; } item = (*THIS->dict)[key]; if (item != NULL) { GB.Error("Key already used: &1", key); return; } if (!MISSING(after)) { akey = GB.ToZeroString(ARG(after)); if (*akey) { after = (*THIS->dict)[akey]; if (after == NULL) { GB.Error("After item does not exist"); return; } } } else after = 0; //WIDGET->last; if (after == NULL) item = new MyListViewItem(THIS, wid); else item = new MyListViewItem(THIS, wid, after); item->setText(0, QSTRING_ARG(text)); GB.StoreString(ARG(key), &item->key); THIS->dict->insert(item->key, item); if (!MISSING(picture)) item->setPicture(ARG(picture)); item->setRenameEnabled(0, THIS->rename); THIS->item = item; RETURN_SELF();END_METHODBEGIN_METHOD(CTREEVIEW_remove, GB_STRING key) MyListViewItem *lvi; char *key = GB.ToZeroString(ARG(key)); //QListView *wid = QLISTVIEW(_object); lvi = CTreeView::getItem(THIS, key); if (!lvi) return; //THIS->dict->remove(key); delete lvi;END_METHODBEGIN_METHOD(CTREEVIEW_exist, GB_STRING key) GB.ReturnBoolean((*THIS->dict)[GB.ToZeroString(ARG(key))] != 0);END_METHODBEGIN_METHOD(CTREEVIEW_find, GB_INTEGER x; GB_INTEGER y) QPoint p(VARG(x), VARG(y)); MyListViewItem *lvi; //qDebug("before: %d %d", p.x(), p.y()); WIDGET->viewport()->mapFrom(WIDGET, p); //qDebug("after: %d %d", p.x(), p.y()); lvi = (MyListViewItem *)WIDGET->itemAt(p); return_item(THIS, lvi);END_METHODBEGIN_METHOD(CTREEVIEW_get, GB_STRING key) MyListViewItem *item; char *key = GB.ToZeroString(ARG(key)); item = CTreeView::getItem(THIS, key); if (!item) return; THIS->item = item; RETURN_SELF();END_METHODBEGIN_PROPERTY(CTREEVIEW_mode) if (READ_PROPERTY) GB.ReturnInteger(WIDGET->selectionMode()); else WIDGET->setSelectionMode((QListView::SelectionMode)VPROP(GB_INTEGER));END_PROPERTYBEGIN_PROPERTY(CTREEVIEW_current) MyListViewItem *item = (MyListViewItem *)(WIDGET->currentItem()); //if (item && (WIDGET->selectionMode() == QListView::Single) && !item->isSelected()) // item = 0; THIS->item = item; if (item == 0) GB.ReturnNull(); else RETURN_SELF();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -