Skip to content

Commit

Permalink
feature NCTree multi selection added (needed for
Browse files Browse the repository at this point in the history
snapper feature #303699)

svn path=/branches/SuSE-Code-11-SP2-Branch/ncurses/; revision=64101
  • Loading branch information
gabi2 committed May 24, 2011
1 parent 3831bba commit 87883f3
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 43 deletions.
163 changes: 124 additions & 39 deletions src/NCTree.cc
Expand Up @@ -37,19 +37,20 @@ class NCTreeLine : public NCTableLine
NCTreeLine * fchild;

mutable chtype * prefix;

bool multiSel;
unsigned prefixLen() const { return level + 3; }

public:

NCTreeLine( NCTreeLine * p, YTreeItem * item )
NCTreeLine( NCTreeLine * p, YTreeItem * item, bool multiSelection )
: NCTableLine( 0 )
, yitem( item )
, level( p ? p->level + 1 : 0 )
, parent( p )
, nsibling( 0 )
, fchild( 0 )
, prefix( 0 )
, multiSel( multiSelection )
{
if ( parent )
{
Expand All @@ -73,9 +74,16 @@ class NCTreeLine : public NCTableLine
}
}

Append( new NCTableCol( NCstring( string( prefixLen(), ' ' )

if ( !multiSel )
{
Append( new NCTableCol( NCstring( string( prefixLen(), ' ' )
+ yitem->label() ) ) );
}
else
{
Append( new NCTableCol( NCstring( string( prefixLen(), ' ' ) + "[ ] "
+ yitem->label() ) ) );
}
}

virtual ~NCTreeLine() { delete [] prefix; }
Expand Down Expand Up @@ -132,13 +140,14 @@ class NCTreeLine : public NCTableLine
switch ( key )
{
case KEY_IC:

case '+':
if ( fchild->isVisible() )
return 0;

break;

case KEY_DC:
case '-':
if ( !fchild->isVisible() )
return 0;

Expand All @@ -147,8 +156,6 @@ class NCTreeLine : public NCTableLine
case KEY_SPACE:
// case KEY_RETURN: see bug 67350

case '+':
case '-':
break;

default:
Expand Down Expand Up @@ -235,11 +242,18 @@ class NCTreeLine : public NCTableLine



NCTree::NCTree( YWidget * parent, const string & nlabel )
: YTree( parent, nlabel )
NCTree::NCTree( YWidget * parent, const string & nlabel, bool multiselection, bool recursiveselection )
: YTree( parent, nlabel, multiselection, recursiveselection )
, NCPadWidget( parent )
, multiSel ( multiselection )
{
yuiDebug() << endl;

if ( multiselection && recursiveselection )
yuiMilestone() << "NCTree recursive multi selection ON" << endl;
else if ( multiselection )
yuiMilestone() << "NCTree multi selection ON" << endl;

setLabel( nlabel );
}

Expand Down Expand Up @@ -336,7 +350,20 @@ YTreeItem * NCTree::getCurrentItem() const
return yitem;
}

void NCTree::deselectAllItems()
{
if ( multiSel)
{
YItemCollection selectedItems = YTree::selectedItems();

for ( YItemConstIterator it = selectedItems.begin(); it != selectedItems.end(); ++it )
{
selectItem( *it, false );
}
}

YTree::deselectAllItems();
}


// Set current item (under the cursor) to selected
Expand All @@ -348,17 +375,48 @@ void NCTree::selectItem( YItem *item, bool selected )
YTreeItem * treeItem = dynamic_cast<YTreeItem *>( item );
YUI_CHECK_PTR( treeItem );
YTreeItem *citem = getCurrentItem();

//retrieve position of item
int at = treeItem->index();

NCTreeLine * cline = 0; // current line
NCTableCol * ccol = 0; // current column

if ( multiSel )
{
cline = modifyTreeLine( at );
if ( cline )
{
ccol = cline->GetCol(0);
}
}

if ( !selected && ( treeItem == citem ) )
if ( !selected )
{
YTree::deselectAllItems();
if ( !multiSel && (treeItem == citem) )
{
YTree::deselectAllItems();
}
else
{
YTree::selectItem ( treeItem, false );
if ( ccol )
{
ccol->SetLabel( NCstring( string( cline->Level() + 3, ' ' ) + "[ ] "
+ item->label() ) );
}
}
}
else
{
//retrieve position of item
int at = treeItem->index();

YTree::selectItem( treeItem, selected );

if ( multiSel && ccol )
{
ccol->SetLabel( NCstring( string( cline->Level() + 3, ' ' ) + "[x] "
+ item->label() ) );
}

//this highlights selected item, possibly unpacks the tree
//should it be in currently hidden branch
myPad()->ShowItem( getTreeLine( at ) );
Expand Down Expand Up @@ -422,7 +480,7 @@ void NCTree::CreateTreeLines( NCTreeLine * parentLine, NCTreePad * pad, YItem *
YTreeItem * treeItem = dynamic_cast<YTreeItem *>( item );
YUI_CHECK_PTR( treeItem );

NCTreeLine * line = new NCTreeLine( parentLine, treeItem );
NCTreeLine * line = new NCTreeLine( parentLine, treeItem, multiSel );
pad->Append( line );

// iterate over children
Expand All @@ -433,15 +491,18 @@ void NCTree::CreateTreeLines( NCTreeLine * parentLine, NCTreePad * pad, YItem *
}
}



// Returns current item (pure virtual in YTree)
YTreeItem * NCTree::currentItem()
{
return getCurrentItem();
}

// Fills TreePad with lines (uses CreateTreeLines to create them)
void NCTree::DrawPad()
{
if ( !myPad() )
{
yuiWarning() << "PadWidget not valid" << endl;
yuiWarning() << "PadWidget not yet created" << endl;
return;
}

Expand All @@ -463,36 +524,60 @@ NCursesEvent NCTree::wHandleInput( wint_t key )
NCursesEvent ret = NCursesEvent::none;
YTreeItem * oldCurrentItem = getCurrentItem();

if ( ! handleInput( key ) ) // NCTreePad::handleInput()
{
switch ( key )
{
case KEY_SPACE: // KEY_SPACE is handled in NCTreeLine::handleInput
case KEY_RETURN:

if ( notify() )
{
return NCursesEvent::Activated;
}
break;
}
}

bool handled = handleInput( key ); // NCTreePad::handleInput()
const YItem * currentItem = getCurrentItem();

if ( !currentItem )
return ret;

YTree::selectItem( const_cast<YItem *>( currentItem ), true );
if ( multiSel )
{
if ( ! handled )
{
switch ( key )
{
// KEY_SPACE is handled in NCTreeLine::handleInput
case KEY_RETURN:

if ( currentItem->selected() )
selectItem( const_cast<YItem *>(currentItem), false );
else
selectItem( const_cast<YItem *>(currentItem), true );

if ( notify() )
{
return NCursesEvent::ValueChanged;
}
break;
}
}
}
else
{
if ( ! handled )
{
switch ( key )
{
// KEY_SPACE is handled in NCTreeLine::handleInput
case KEY_RETURN:

if ( notify() )
{
return NCursesEvent::Activated;
}
break;
}
}

yuiDebug() << "Old item: " << oldCurrentItem->label() << " Current: " << currentItem->label() << endl;
YTree::selectItem( const_cast<YItem *>( currentItem ), true );
}

if ( notify() && immediateMode() && ( oldCurrentItem != currentItem ) )
ret = NCursesEvent::SelectionChanged;


yuiDebug() << "Notify: " << ( notify() ? "true" : "false" ) << " Return event: " << ret << endl;
ret = NCursesEvent::SelectionChanged;

yuiDebug() << "Notify: " << ( notify() ? "true" : "false" ) <<
" Return event: " << ret.reason << endl;

return ret;
}

Expand Down
9 changes: 8 additions & 1 deletion src/NCTree.h
Expand Up @@ -24,6 +24,7 @@
#include "YTree.h"
#include "NCPadWidget.h"
#include "NCTreePad.h"
#include "NCTablePad.h"

class NCTreeLine;

Expand All @@ -37,6 +38,8 @@ class NCTree : public YTree, public NCPadWidget
NCTree( const NCTree & );

int idx;
bool multiSel;

void CreateTreeLines( NCTreeLine * p, NCTreePad * pad, YItem * item );

protected:
Expand All @@ -57,7 +60,7 @@ class NCTree : public YTree, public NCPadWidget

public:

NCTree( YWidget * parent, const string & label );
NCTree( YWidget * parent, const string & label, bool multiselection=false, bool recursiveselection=false );
virtual ~NCTree();

virtual int preferredWidth();
Expand All @@ -70,6 +73,10 @@ class NCTree : public YTree, public NCPadWidget

virtual YTreeItem * getCurrentItem() const;

virtual YTreeItem * currentItem();

virtual void deselectAllItems();

virtual void selectItem( YItem *item, bool selected );
virtual void selectItem( int index );

Expand Down
4 changes: 2 additions & 2 deletions src/NCWidgetFactory.cc
Expand Up @@ -153,9 +153,9 @@ NCWidgetFactory::createSelectionBox( YWidget * parent, const string & label )


NCTree *
NCWidgetFactory::createTree( YWidget * parent, const string & label )
NCWidgetFactory::createTree( YWidget * parent, const string & label, bool multiselection, bool recursiveselection )
{
NCTree * tree = new NCTree( parent, label );
NCTree * tree = new NCTree( parent, label, multiselection, recursiveselection );
YUI_CHECK_NEW( tree );

return tree;
Expand Down
2 changes: 1 addition & 1 deletion src/NCWidgetFactory.h
Expand Up @@ -93,7 +93,7 @@ class NCWidgetFactory: public YWidgetFactory
virtual NCRadioButton * createRadioButton ( YWidget * parent, const string & label, bool isChecked = false );
virtual NCComboBox * createComboBox ( YWidget * parent, const string & label, bool editable = false );
virtual NCSelectionBox * createSelectionBox ( YWidget * parent, const string & label );
virtual NCTree * createTree ( YWidget * parent, const string & label );
virtual NCTree * createTree ( YWidget * parent, const string & label, bool multiselection = false, bool recursiveselection = false );
virtual NCTable * createTable ( YWidget * parent, YTableHeader * tableHeader, bool multiSelection = false );
virtual NCProgressBar * createProgressBar ( YWidget * parent, const string & label, int maxValue = 100 );
virtual NCRichText * createRichText ( YWidget * parent, const string & text = string(), bool plainTextMode = false );
Expand Down

0 comments on commit 87883f3

Please sign in to comment.