summaryrefslogtreecommitdiffstats
path: root/kmix/viewapplet.cpp
blob: 0615c85e3400ca54575a940a6b11b93f514c47f6 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * KMix -- KDE's full featured mini mixer
 *
 *
 * Copyright (C) 1996-2004 Christian Esken <esken@kde.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "viewapplet.h"

// TQt
#include <tqwidget.h>
#include <layout.h>
#include <tqsize.h>

// KDE
#include <kactioncollection.h>
#include <kdebug.h>
#include <kpanelapplet.h>
#include <kstdaction.h>

// KMix
#include "kmixtoolbox.h"
#include "mdwslider.h"
#include "mixer.h"

ViewApplet::ViewApplet(TQWidget* parent, const char* name, Mixer* mixer, ViewBase::ViewFlags vflags, KPanelApplet::Position position )
    : ViewBase(parent, name, TQString(), mixer, WStyle_Customize|WStyle_NoBorder, vflags)
{
    setBackgroundOrigin(AncestorOrigin);
    // remove the menu bar action, that is put by the "ViewBase" constructor in _actions.
    //KToggleAction *m = static_cast<KToggleAction*>(KStdAction::showMenubar( this, TQT_SLOT(toggleMenuBarSlot()), _actions ));
    _actions->remove( KStdAction::showMenubar(TQT_TQOBJECT(this), TQT_SLOT(toggleMenuBarSlot()), _actions) );


    if ( position == KPanelApplet::pLeft || position == KPanelApplet::pRight ) {
      //kdDebug(67100) << "ViewApplet() isVertical" << "\n";
      _viewOrientation = Qt::Vertical;
    }
     else {
      //kdDebug(67100) << "ViewApplet() isHorizontal" << "\n";
      _viewOrientation = Qt::Horizontal;
    }

    if ( _viewOrientation == Qt::Horizontal ) {
	_layoutMDW = new TQHBoxLayout( this );
	setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Preferred);
    }
    else {
	_layoutMDW = new TQVBoxLayout( this );
	setSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed);
    }


    //_layoutMDW->setResizeMode(TQLayout::Fixed);
    init();
}

ViewApplet::~ViewApplet() {
}

void ViewApplet::setMixSet(MixSet *mixset)
{
    MixDevice* md;
    for ( md = mixset->first(); md != 0; md = mixset->next() ) {
	if ( (! md->isSwitch()) && ( ! md->isEnum() ) ) {
	    _mixSet->append(md);
	}
    }
}

int ViewApplet::count()
{
    return ( _mixSet->count() );	
}

int ViewApplet::advice() {
    if (  _mixSet->count() > 0 ) {
        // The standard input and output views are always advised, if there are devices in it
        return 100;
    }
    else {
        return 0;
    }
}



TQWidget* ViewApplet::add(MixDevice *md)
{
    /**
       Slider orientation is exactly the other way round. If the applet stretches horzontally,
       the sliders must be vertical
    */
    Qt::Orientation sliderOrientation;
    if (_viewOrientation == Qt::Horizontal )
        sliderOrientation = Qt::Vertical;
    else
        sliderOrientation = Qt::Horizontal;
	
    //    kdDebug(67100) << "ViewApplet::add()\n";
    MixDeviceWidget *mdw =
        new MDWSlider(
                _mixer,       // the mixer for this device
                md,           // MixDevice (parameter)
                false,        // Show Mute LED
                false,        // Show Record LED
                true,         // Small
                sliderOrientation, // Qt::Orientation
                this,         // parent
                this,         // View widget
                md->name().latin1()
                );
    mdw->setBackgroundOrigin(AncestorOrigin);
    
    static_cast<MDWSlider*>(mdw)->setValueStyle(MixDeviceWidget::NNONE);
    static_cast<MDWSlider*>(mdw)->setIcons(shouldShowIcons( size()) ); // !!! This should use the panel size
    _layoutMDW->add(mdw);
    return mdw;
}

void ViewApplet::constructionFinished() {
    _layoutMDW->activate();
    
    KMixToolBox::setIcons     ( _mdws, shouldShowIcons( size()) ); // !!! This should use the panel size
    KMixToolBox::setValueStyle( _mdws, MixDeviceWidget::NNONE);
}


TQSize ViewApplet::sizeHint() const {
    // Basically out main layout knows very good what the sizes should be
    TQSize qsz = _layoutMDW->sizeHint();
    //kdDebug(67100) << "ViewApplet::sizeHint(): NewSize is " << qsz << "\n";
    return qsz;
}

TQSizePolicy ViewApplet::sizePolicy() const {
    if ( _viewOrientation == Qt::Horizontal ) {
	//kdDebug(67100) << "ViewApplet::sizePolicy=(Fixed,Expanding)\n";
	return TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Expanding);
    }
    else {
	//kdDebug(67100) << "ViewApplet::sizePolicy=(Expanding,Fixed)\n";
	return TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Fixed);
    }
}

bool ViewApplet::shouldShowIcons(TQSize qsz) {
    bool showIcons = false;
    if ( _viewOrientation == Qt::Horizontal ) {
        if ( qsz.height() >= 32 ) {
            //kdDebug(67100) << "ViewApplet::resizeEvent() hor >=32" << qre->size() << "\n";
            showIcons = true;
        }
    }
    else {
       if ( qsz.width() >= 32 ) {
           //kdDebug(67100) << "ViewApplet::resizeEvent() vert >=32" << qre->size() << "\n";
           showIcons = true;
       }
    }
    return showIcons;
}

void ViewApplet::resizeEvent(TQResizeEvent *qre)
{
    //kdDebug(67100) << "ViewApplet::resizeEvent() size=" << qre->size() << "\n";
    // decide whether we have to show or hide all icons
    bool showIcons = shouldShowIcons(qre->size());

    for ( TQWidget *mdw = _mdws.first(); mdw != 0; mdw = _mdws.next() ) {
	if ( mdw == 0 ) {
	    kdError(67100) << "ViewApplet::resizeEvent(): mdw == 0\n";
	    break; // sanity check (normally the lists are set up correctly)
	}
	else {
	    if ( mdw->inherits("MDWSlider")) {
		static_cast<MDWSlider*>(mdw)->setIcons(showIcons);
		static_cast<MDWSlider*>(mdw)->setValueStyle(MixDeviceWidget::NNONE);
		//static_cast<MDWSlider*>(mdw)->resize(qre->size());
	    }
	}
    }
    //    kdDebug(67100) << "ViewApplet::resizeEvent(). SHOULD resize _layoutMDW to " << qre->size() << endl;
    //TQWidget::resizeEvent(qre);

    // resizing changes our own sizeHint(), because we must take the new PanelSize in account.
    // So updateGeometry() is amust for us.
    updateGeometry();
}


void ViewApplet::refreshVolumeLevels() {
    //kdDebug(67100) << "ViewApplet::refreshVolumeLevels()\n";

     TQWidget *mdw = _mdws.first();
     MixDevice* md;
     for ( md = _mixSet->first(); md != 0; md = _mixSet->next() ) {
	 if ( mdw == 0 ) {
	     kdError(67100) << "ViewApplet::refreshVolumeLevels(): mdw == 0\n";
	     break; // sanity check (normally the lists are set up correctly)
	 }
	 else {
	     if ( mdw->inherits("MDWSlider")) {
		 //kdDebug(67100) << "ViewApplet::refreshVolumeLevels(): updating\n";
		 // a slider, fine. Lets update its value
		 static_cast<MDWSlider*>(mdw)->update();
	     }
	     else {
		 kdError(67100) << "ViewApplet::refreshVolumeLevels(): mdw is not slider\n";
		 // no slider. Cannot happen in theory => skip it
	     }
	 }
	 mdw = _mdws.next();
    }
}

void ViewApplet::configurationUpdate() {
    updateGeometry();
    //_layoutMDW->activate();
    constructionFinished(); // contains "_layoutMDW->activate();"
    emit appletContentChanged();
    kdDebug(67100) << "ViewApplet::configurationUpdate()" << endl;
    // the following "emit" is only here to be picked up by KMixApplet, because it has to
    // - make sure the panel is informed about the size change
    // - save the new configuration
    //emit configurationUpdated();
}

#include "viewapplet.moc"