File: dialogselectmaster.cpp

package info (click to toggle)
kmix 4%3A20.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,504 kB
  • sloc: cpp: 14,313; xml: 408; sh: 97; makefile: 7
file content (246 lines) | stat: -rw-r--r-- 9,133 bytes parent folder | download
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
243
244
245
246
/*
 * 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 "gui/dialogselectmaster.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QListWidget>
#include <QComboBox>
#include <QToolTip>

#include <klocalizedstring.h>

#include "core/ControlManager.h"
#include "core/mixer.h"
#include "gui/kmixtoolbox.h"


// TOD: Should this be incorporated into the "Configure KMix" dialogue?

DialogSelectMaster::DialogSelectMaster(const Mixer *mixer, QWidget *parent)
    : DialogBase(parent)
{
    setWindowTitle(i18n("Select Master Channel"));
    if (Mixer::mixers().count()>0) setButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
    else setButtons(QDialogButtonBox::Cancel);

   m_channelSelector = nullptr;
   createWidgets(mixer);				// for the specified 'mixer'
}


/**
 * Create basic widgets of the dialogue.
 */
void DialogSelectMaster::createWidgets(const Mixer *mixer)
{
    QWidget *mainFrame = new QWidget(this);
    setMainWidget(mainFrame);
    QVBoxLayout *layout = new QVBoxLayout(mainFrame);

    const QList<Mixer *> &mixers = Mixer::mixers();	// list of all mixers present

    if (mixers.count()>1)
    {
        int mixerIndex = 0;				// index of selected mixer

        // TODO: should "Playback Streams" (ALSA) be shown here?
        // There is no meaningful concept of a master channel for that.

        // More than one Mixer => show Combo-Box to select Mixer
        // Mixer widget line
        QHBoxLayout *mixerNameLayout = new QHBoxLayout();
        layout->addLayout( mixerNameLayout );
        mixerNameLayout->setContentsMargins(0, 0, 0, 0);
    
        QLabel *qlbl = new QLabel( i18n("Current mixer:"), mainFrame );
        mixerNameLayout->addWidget(qlbl);
        qlbl->setFixedHeight(qlbl->sizeHint().height());
    
        m_cMixer = new QComboBox(mainFrame);
        m_cMixer->setObjectName( QLatin1String( "mixerCombo" ) );
        m_cMixer->setFixedHeight(m_cMixer->sizeHint().height());
        connect(m_cMixer, QOverload<int>::of(&QComboBox::activated), this, &DialogSelectMaster::createPageByID);

        for (int i = 0; i<mixers.count(); ++i)
        {
            const Mixer *m = mixers[i];
            m_cMixer->addItem(QIcon::fromTheme(m->iconName()), m->readableName(), m->id());
            if (m->id()==mixer->id()) mixerIndex = i;
         }

        // Select the specified 'mixer' as the current item in the combo box.
        // If it was not found by the loop above, then select the first item.
        m_cMixer->setCurrentIndex(mixerIndex);
        
        mixerNameLayout->addWidget(m_cMixer, 1);
        layout->addSpacing(DialogBase::verticalSpacing());
    }

    if (mixers.count()>0)
    {
        QLabel *qlbl = new QLabel(xi18nc("@info", "Select the <a href=\"i\">master volume</a> channel:"), mainFrame);
        // Not a good idea to set Qt::LinksAccessibleByKeyboard, if the link is
        // clicked on or gets focus then the Return key will not activate the OK
        // button as expected.
        //qlbl->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::LinksAccessibleByKeyboard);
        layout->addWidget(qlbl);
        connect(qlbl, &QLabel::linkActivated, this, [this]() {
            QToolTip::showText(QCursor::pos(), xi18nc("@info:tooltip",
"<para>Here you can select the master sound device (if there is more than one) and its master channel.</para>"
"<para>The master channel is the one that is affected by the system tray volume control, "
"the volume up/down and mute global shortcut keys, "
"and the <interface>Mute</interface> action in the system tray popup menu.</para>"));
        });

        createPage(mixer);

        connect(this, &QDialog::accepted, this, &DialogSelectMaster::apply);
    }
    else
    {
	QWidget *noMixersWarning = KMixToolBox::noDevicesWarningWidget(mainFrame);
        layout->addWidget(noMixersWarning);
        layout->addStretch(1);
    }
}


/**
 * Create the channel list for the specified mixer
 *
 * @p mixerId the ID (index) of the mixer for which the list should be created.
 */
void DialogSelectMaster::createPageByID(int mixerId)
{
    // TODO: would Mixer::mixers().value(mixerId) work just as well?
    // TODO: mixerId should really be named mixerIndex!
    const QString mixer_id = m_cMixer->itemData(mixerId).toString();
    const Mixer *mixer = Mixer::findMixer(mixer_id);
    if (mixer!=nullptr) createPage(mixer);
}


/**
 * Create the channel list for the specified mixer
 *
 * @p mixer the mixer for which the list should be created
 */
void DialogSelectMaster::createPage(const Mixer *mixer)
{
    /** --- Reset page -----------------------------------------------
     * In case the user selected a new Mixer via m_cMixer, we need
     * to remove the stuff created on the last call.
     */
	// delete the list widget.
	// This should automatically remove all contained items.
	delete m_channelSelector;
    
    /** Reset page end -------------------------------------------------- */
    
        QWidget *mainFrame = mainWidget();
        QVBoxLayout *layout = qobject_cast<QVBoxLayout *>(mainFrame->layout());
        Q_ASSERT(layout!=nullptr);

	m_channelSelector = new QListWidget(mainFrame);
#ifndef QT_NO_ACCESSIBILITY
        m_channelSelector->setAccessibleName( i18n("Select Master Channel") );
#endif
	m_channelSelector->setSelectionMode(QAbstractItemView::SingleSelection);
	m_channelSelector->setDragEnabled(false);
	m_channelSelector->setAlternatingRowColors(true);
	layout->addWidget(m_channelSelector);

        const MixSet &mixset = mixer->getMixSet();
	const MasterControl mc = mixer->getGlobalMasterPreferred(false);
	QString masterKey = mc.getControl();
	if (!masterKey.isEmpty() && !mixset.get(masterKey))
	{
		const shared_ptr<MixDevice> master = mixer->getLocalMasterMD();
		if (master.get()!=nullptr) masterKey = master->id();
	}

	int msetCount = 0;
	for (int i = 0; i<mixset.count(); ++i)
        {
            const shared_ptr<MixDevice> md = mixset[i];
            if (md->playbackVolume().hasVolume()) ++msetCount;
        }

	if (msetCount>0 && !mixer->isDynamic())
	{
            QString mdName = i18n("Automatic (%1 recommendation)", mixer->getDriverName());
            auto *item = new QListWidgetItem(QIcon::fromTheme("audio-volume-high"), mdName, m_channelSelector);
            item->setData(Qt::UserRole, QString());	// ID here: see apply(), empty String => Automatic
            if (masterKey.isEmpty()) m_channelSelector->setCurrentItem(item);
	}

	// Populate the list view with the MixDevice's having a playback volume
        // (excludes pure capture controls and pure enum's)
	for (int i = 0; i<mixset.count(); ++i)
        {
            const shared_ptr<MixDevice> md = mixset[i];
            if (md->playbackVolume().hasVolume())
            {
                const QString mdName = md->readableName();
                auto *item = new QListWidgetItem(QIcon::fromTheme(md->iconName()), mdName, m_channelSelector);
                item->setData(Qt::UserRole, md->id());  // ID here: see apply()
                if (md->id() == masterKey)
                {					// select the current master
                    m_channelSelector->setCurrentItem(item);
                }
            }
        }

        setButtonEnabled(QDialogButtonBox::Ok, m_channelSelector->count()>0);
}


void DialogSelectMaster::apply()
{
    const QList<Mixer *> &mixers = Mixer::mixers();	// list of all mixers present
    Mixer *mixer = nullptr;				// selected mixer found

    if (mixers.count()==1)
    {
        // only one mixer => no combo box => take first entry
        mixer = mixers.first();
    }
    else if (mixers.count()>1)
    {
        // use the mixer that is currently selected in the combo box
        const int idx = m_cMixer->currentIndex();
        if (idx!=-1) mixer = mixers.at(idx);
    }

    if (mixer==nullptr) return;				// user must have unplugged everything

    QList<QListWidgetItem *> items = m_channelSelector->selectedItems();
    if (items.count()==1)
    {
    	QListWidgetItem *item = items.first();
    	QString control_id = item->data(Qt::UserRole).toString();
        mixer->setLocalMasterMD(control_id);
        Mixer::setGlobalMaster(mixer->id(), control_id, true);
        ControlManager::instance().announce(mixer->id(), ControlManager::MasterChanged, QString("Select Master Dialog"));
    }
}