summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmtexturemapedit.cpp
blob: efb77c15f6f3e32d6c6f0ddbf40feaf6a40e6374 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2001 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

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


#include "pmtexturemapedit.h"
#include "pmtexturemap.h"
#include "pmlineedits.h"

#include <tqlayout.h>
#include <tqlabel.h>
#include <klocale.h>
#include <kmessagebox.h>


PMTextureMapEdit::PMTextureMapEdit( TQWidget* tqparent, const char* name )
      : Base( tqparent, name )
{
   m_pDisplayedObject = 0;
   m_numValues = 0;
}

void PMTextureMapEdit::createTopWidgets( )
{
   Base::createTopWidgets( );
   topLayout( )->addWidget( new TQLabel( i18n( "Map values:" ), this ) );
   m_pNoChildLabel = new TQLabel( i18n( "(No Child Objects)" ), this );
   m_pPureLinkLabel = new TQLabel( i18n( "(Pure Link)" ), this );
   topLayout( )->addWidget( m_pNoChildLabel );
   topLayout( )->addWidget( m_pPureLinkLabel );
   TQHBoxLayout* hl = new TQHBoxLayout( topLayout( ) );
   m_pEditLayout = new TQVBoxLayout( hl );
   hl->addStretch( 1 );
}

void PMTextureMapEdit::displayObject( PMObject* o )
{
   TQString str;

   if( o->isA( "TextureMapBase" ) )
   {
      bool readOnly = o->isReadOnly( );
      m_pDisplayedObject = ( PMTextureMapBase* ) o;
      TQValueList<double> mv = m_pDisplayedObject->mapValues( );
      TQValueList<double>::Iterator vit = mv.begin( );
      TQPtrListIterator<PMFloatEdit> eit( m_edits );
      PMFloatEdit* edit;

      m_numValues = 0;

      for( ; vit != mv.end( ); ++vit )
      {
         if( eit.current( ) )
         {
            eit.current( )->setValue( *vit );
            eit.current( )->show( );
            eit.current( )->setReadOnly( readOnly );
            ++eit;
         }
         else
         {
            edit = new PMFloatEdit( this );
            m_pEditLayout->addWidget( edit );
            m_edits.append( edit );
            edit->setValue( *vit );
            edit->setValidation( true, 0.0, true, 1.0 );
            edit->setReadOnly( readOnly );
            connect( edit, TQT_SIGNAL( dataChanged( ) ), TQT_SIGNAL( dataChanged( ) ) );
         }
         m_numValues++;
      }
      for( ; eit.current( ); ++eit )
         eit.current( )->hide( );
      if( m_numValues == 0 )
      {
         if( o->linkedObject( ) )
         {
            m_pPureLinkLabel->show( );
            m_pNoChildLabel->hide( );
         }
         else
         {
            m_pPureLinkLabel->hide( );
            m_pNoChildLabel->show( );
         }
      }
      else
      {
         m_pNoChildLabel->hide( );
         m_pPureLinkLabel->hide( );
      }
   }
   else
      kdError( PMArea ) << "PMTextureMapEdit: Can't display object\n";
   Base::displayObject( o );
   enableLinkEdit( m_numValues == 0 );
}

void PMTextureMapEdit::saveContents( )
{
   if( m_pDisplayedObject )
   {
      if( m_numValues > 0 )
      {
         TQPtrListIterator<PMFloatEdit> it( m_edits );
         int i = 0;
         TQValueList<double> values;
         
         for( ; ( i < m_numValues ) && it.current( ); ++i, ++it )
            values.append( it.current( )->value( ) );
         m_pDisplayedObject->setMapValues( values );
      }
      Base::saveContents( );
   }
}

bool PMTextureMapEdit::isDataValid( )
{
   TQPtrListIterator<PMFloatEdit> it( m_edits );
   int i = 0;
   double last = 0.0;

   for( ; ( i < m_numValues ) && it.current( ); ++i, ++it )
   {
      if( !it.current( )->isDataValid( ) )
         return false;
      if( it.current( )->value( ) < last )
      {
         KMessageBox::error( this, i18n( "The map values have to be increasing." ),
                             i18n( "Error" ) );
         it.current( )->setFocus( );
         return false;
      }
      last = it.current( )->value( );
   }
   return Base::isDataValid( );
}

#include "pmtexturemapedit.moc"