summaryrefslogtreecommitdiffstats
path: root/kig/modes/linkslabel.cpp
blob: 8d711d65201b978f230a2299e8011fbd848f21ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (C)  2002  Dominique Devriese <devriese@kde.org>

// This library 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 2.1 of the License, or (at your option) any later version.

// This library 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 this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301  USA


#include "linkslabel.h"
#include "linkslabel.moc"

#include <tqlabel.h>
#include <kurllabel.h>
#include <tqlayout.h>

#include <vector>
#include <algorithm>
#include <functional>

#include <assert.h>
using namespace std;

class LinksLabel::Private
{
public:
  TQHBoxLayout* tqlayout;
  std::vector<TQLabel*> labels;
  std::vector<KURLLabel*> urllabels;
};

LinksLabel::LinksLabel( TQWidget* tqparent, const char* name )
  : TQWidget( tqparent, name )
{
  p = new Private;
  p->tqlayout = new TQHBoxLayout( this );

  TQLabel* l = new TQLabel( TQString::fromUtf8( "Dit is een " ), this );
  p->labels.push_back( l );
  p->tqlayout->addWidget( l );

  KURLLabel* u = new KURLLabel( TQString::fromUtf8( "http://www.kde.org/" ),
                                TQString::fromUtf8( "url"), this );
  p->urllabels.push_back( u );
  p->tqlayout->addWidget( u );

  l = new TQLabel( TQString::fromUtf8( " !" ), this );
  p->labels.push_back( l );
  p->tqlayout->addWidget(l );

  p->tqlayout->activate();
}

LinksLabel::~LinksLabel()
{
  delete p;
}

void LinksLabel::urlClicked()
{
  const TQObject* o = TQT_TQOBJECT_CONST(const_cast<const TQT_BASE_OBJECT_NAME*>(sender()));
  std::vector<KURLLabel*>::iterator i = std::find( p->urllabels.begin(), p->urllabels.end(), static_cast<const KURLLabel*>(TQT_TQWIDGET_CONST( o  )) );
  assert( i != p->urllabels.end() );
  emit linkClicked( i - p->urllabels.begin() );
}

LinksLabel::LinksLabelEditBuf LinksLabel::startEdit()
{
  return LinksLabelEditBuf();
}

void LinksLabel::addText( const TQString& s, LinksLabelEditBuf& buf )
{
  buf.data.push_back( std::pair<bool, TQString>( false, s ) );
}

void LinksLabel::addLink( const TQString& s, LinksLabelEditBuf& buf )
{
  buf.data.push_back( std::pair<bool, TQString>( true, s ) );
}

namespace {
  void deleteObj( TQWidget* o ) { delete o; }
}

void LinksLabel::applyEdit( LinksLabelEditBuf& buf )
{
  std::for_each( p->urllabels.begin(), p->urllabels.end(), deleteObj );
  std::for_each( p->labels.begin(), p->labels.end(), deleteObj );
  p->urllabels.clear();
  p->labels.clear();

  delete p->tqlayout;
  p->tqlayout = new TQHBoxLayout( this );

  for ( LinksLabelEditBuf::vec::iterator i = buf.data.begin(); i != buf.data.end(); ++i )
  {
    if ( i->first )
    {
      // we need a KURLLabel...
      // the url is an unused stub...
      KURLLabel* l = new KURLLabel( TQString::fromUtf8( "http://edu.kde.org/kig" ),
                                    i->second, this );
      p->urllabels.push_back( l );
      p->tqlayout->addWidget( l );
      connect( l, TQT_SIGNAL( leftClickedURL() ), TQT_SLOT( urlClicked() ) );
    }
    else
    {
      // we need a normal label...
      TQLabel* l = new TQLabel( i->second, this );
      p->labels.push_back( l );
      p->tqlayout->addWidget( l );
    };
  };

  TQSpacerItem* spacer = new TQSpacerItem( 40,  20,  TQSizePolicy::Expanding,  TQSizePolicy::Minimum );

  p->tqlayout->addItem( spacer );

  p->tqlayout->activate();

  std::for_each( p->urllabels.begin(), p->urllabels.end(), mem_fun( &TQWidget::show ) );
  std::for_each( p->labels.begin(), p->labels.end(), mem_fun( &TQWidget::show ) );
}