WQtTreeItem.cpp 7.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
25
#include <set>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
26
#include <string>
27

28 29 30 31
#include <QtGui/QApplication>

#include "../../../kernel/WKernel.h"

32 33 34
#include "../../../common/WProgressCombiner.h"
#include "../../../common/WProgress.h"

35 36 37
#include "../../../kernel/WModuleInputConnector.h"
#include "../../../kernel/WModuleOutputConnector.h"

38
#include "../events/WModuleDeleteEvent.h"
39 40 41
#include "../events/WEventTypes.h"
#include "../events/WPropertyChangedEvent.h"

42 43 44
#include "../WQt4Gui.h"
#include "../WMainWindow.h"

45
#include "WTreeItemTypes.h"
46 47
#include "WQtTreeItem.h"

48
WQtTreeItem::WQtTreeItem( QTreeWidgetItem * parent, WTreeItemType type, boost::shared_ptr< WModule > module ) :
49 50
    QTreeWidgetItem( parent, type ),
    m_deleteInProgress( false ),
51 52
    m_needPostDeleteEvent( true ),
    m_handledInput( "" )
53 54
{
    m_module = module;
55
    m_name = module->getName();
56

57
    if ( module->getProperties()->getProperty( "active" )->toPropBool()->get() )
58 59 60 61 62 63 64 65 66 67
    {
        this->setCheckState( 0, Qt::Checked );
    }
    else
    {
        this->setCheckState( 0, Qt::Unchecked );
    }

    this->setFlags( Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    // grab the runtime name property
    // replace the name by the filename
    boost::shared_ptr< WPropertyBase > p = module->getProperties()->findProperty( "Name" );

    // always ensure that findProperty really found something
    if ( p )
    {
        m_nameProp = p->toPropString();
    }

    // was it a string prop?
    if ( m_nameProp )
    {
        m_name = m_nameProp->get( true );
        m_nameProp->getUpdateCondition()->subscribeSignal( boost::bind( &WQtTreeItem::nameChanged, this ) );
    }

85 86
    m_updateTimer = boost::shared_ptr< QTimer >( new QTimer() );
    connect( m_updateTimer.get(), SIGNAL( timeout() ), this, SLOT( update() ) );
87
    m_updateTimer->start( 50 );
88 89 90 91 92 93 94 95 96 97 98
}

WQtTreeItem::~WQtTreeItem()
{
}

boost::shared_ptr< WModule > WQtTreeItem::getModule()
{
    return m_module;
}

99 100 101 102 103
std::string WQtTreeItem::getName()
{
    return m_name;
}

104 105
void WQtTreeItem::updateTooltip( std::string progress )
{
106 107 108 109 110 111
    std::string tooltip = "";
    if ( m_module->isCrashed()() )
    {
        tooltip += "<b>A problem occured. The module has been stopped. </b><br/><br/>";
    }
    tooltip += "<b>Module: </b>" + m_module->getName() + "<br/>";
112 113 114 115 116
    tooltip += "<b>Progress: </b>" + progress + "<br/>";
    tooltip += "<b>Connectors: </b>";

    // also list the connectors
    std::string conList = "";
117
    WModule::InputConnectorList consIn = m_module->getInputConnectors();
118
    WModule::OutputConnectorList consOut = m_module->getOutputConnectors();
119
    conList += "<table><tr><th>Name</th><th>Description</th><th>Type (I/O)</th><th>Connected</th></tr>";
120
    int conCount = 0;
121
    for ( WModule::InputConnectorList::const_iterator it = consIn.begin(); it != consIn.end(); ++it )
122 123
    {
        ++conCount;
124 125 126
        conList += "<tr><td><b>" + ( *it )->getName() + "&nbsp;</b></td><td>" + ( *it )->getDescription() + "&nbsp;</td>";
        conList += "<td><center>In</center></td>";
        conList += ( *it )->isConnected() ? "<td><center>Yes</center></td>" : "<td><center>No</center></td>";
127 128
        conList += "</tr>";
    }
129
    for ( WModule::OutputConnectorList::const_iterator it = consOut.begin(); it != consOut.end(); ++it )
130 131
    {
        ++conCount;
132 133 134
        conList += "<tr><td><b>" + ( *it )->getName() + "&nbsp;</b></td><td>" + ( *it )->getDescription() + "&nbsp;</td>";
        conList += "<td><center>Out</center></td>";
        conList += ( *it )->isConnected() ? "<td></center>Yes</center></td>" : "<td><center>No</center></td>";
135 136
        conList += "</tr>";
    }
137 138
    conList += "</table>";

139
    tooltip += conCount ? "Yes" + conList + "<br/><br/>" : "None<br/>";
140
    tooltip += "<b>Module Description: </b><br/>" + m_module->getDescription();
141

142 143 144
    setToolTip( 0, tooltip.c_str() );
}

145
void WQtTreeItem::update()
146 147 148 149 150
{
    updateState();
}

void WQtTreeItem::updateState()
151 152 153 154 155 156
{
    boost::shared_ptr< WProgressCombiner> p = m_module->getRootProgressCombiner();

    // update the progress combiners internal state
    p->update();

157 158 159
    std::string connInfo = "";
    if ( ( m_handledOutput != "" ) && ( m_handledInput != "" ) )
    {
160
        connInfo = " (" + m_handledOutput + "->" + m_handledInput + ") ";
161 162
    }

163
    // is it pending?
164
    std::string progress = "Waiting";
165 166
    if ( m_module->isCrashed()() )
    {
167
        setText( 0, ( m_name + " (problem occurred)"  + connInfo ).c_str() );
168 169 170 171 172 173 174 175 176

        // strike out the name of the module to show the crash visually.
        QFont curFont = font( 0 );
        curFont.setStrikeOut( true );
        setFont( 0, curFont );
        setIcon( 0, WQt4Gui::getMainWindow()->getIconManager()->getIcon( "moduleCrashed" ) );

        // this ensures that crashed modules can be deleted
        setDisabled( false );
177 178
    }
    else if ( p->isPending() )
179
    {
180
        setIcon( 0, WQt4Gui::getMainWindow()->getIconManager()->getIcon( "moduleBusy" ) );
181 182 183
        std::ostringstream title;
        if ( p->isDetermined() )
        {
184 185
            title.setf( std::ios::fixed );
            title.precision( 0 );
186
            title << p->getProgress() << "%";
187 188 189
        }
        else
        {
190
            title << "Pending";
191 192
        }

193
        setText( 0, ( m_name + " - " + title.str() + connInfo ).c_str() );
194 195 196
    }
    else
    {
197
        setIcon( 0, QIcon() );
198
        setText( 0, ( m_name + connInfo ).c_str() );
199
    }
200

201 202 203 204 205 206 207 208
    // if the user requested it to be deleted: disable and color it
    if ( m_deleteInProgress )
    {
        setForeground( 0, QBrush( QColor::fromRgb( 255, 0, 0 ) ) );
        setDisabled( true );
    }

    // is finished?
209
    if ( m_deleteInProgress && !m_module->isRunning().get() && m_needPostDeleteEvent )
210 211 212 213 214
    {
        m_needPostDeleteEvent = false;  // this ensures the event is only posted once
        QCoreApplication::postEvent( WQt4Gui::getMainWindow()->getDatasetBrowser(), new WModuleDeleteEvent( this ) );
    }

215 216
    // update tooltip
    updateTooltip( progress );
217 218
}

219
void WQtTreeItem::gotRemoved()
220
{
221
    // this ensures a visual feedback to the user while waiting for the module to finish.
222 223 224 225 226

    // update tree item state
    m_deleteInProgress = true;
}

227 228 229 230 231 232
void WQtTreeItem::nameChanged()
{
    // luckily, the update mechanism of WQtTreeItem regularly sets the name using m_name. So we do not even need to post some kind of event.
    m_name = m_nameProp->get( true );
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
std::string WQtTreeItem::getHandledInput() const
{
    return m_handledInput;
}

void WQtTreeItem::setHandledInput( std::string in )
{
    m_handledInput = in;
}

std::string WQtTreeItem::getHandledOutput() const
{
    return m_handledOutput;
}

void WQtTreeItem::setHandledOutput( std::string out )
{
    m_handledOutput = out;
}