aboutsummaryrefslogblamecommitdiff
path: root/src/plugins/Ui/skinned/togglebutton.cpp
blob: e03bcf334ce88884feee8cbe8641317d7e0b4d98 (plain) (tree)
1
2
3
4
5
6
7
                                                                            
                                                                            

                                                                            


                                                                            












                                                                            
                                                                            



                                                                             
                      







                                                                                        
                
                           
                        














                                                                           
                       

 


                          
                      


                       
                                         








                                                   
                     
                   







                                                     

                        
                           


                            
                           





                                                     
                          




                                                                 
                           




                                                      
 
/***************************************************************************
 *   Copyright (C) 2007-2008 by Ilya Kotov                                 *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   Based on Promoe, an XMMS2 Client                                      *
 *   Copyright (C) 2005-2006 by XMMS2 Team                                 *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU 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 "skin.h"
#include "togglebutton.h"
#include <QMouseEvent>

ToggleButton::ToggleButton ( QWidget *parent,uint on_n,uint on_p,uint off_n,uint off_p )
      : PixmapWidget ( parent )
{
   m_on_n = on_n;
   m_on_p = on_p;
   m_off_n = off_n;
   m_off_p = off_p;
   m_on = false;
   skin = Skin::instance();
   setChecked ( false );
   connect ( skin, SIGNAL ( skinChanged() ), this, SLOT ( updateSkin() ) );
}


ToggleButton::~ToggleButton()
{}

bool ToggleButton::isChecked()
{
   return m_on;
}

void ToggleButton::updateSkin()
{
   //setPixmap ( skin->getButton ( name_normal ) );
   setChecked ( m_on );
}

void ToggleButton::click()
{
    m_on = !m_on;
    setChecked (m_on);
    emit clicked(m_on);
}

void ToggleButton::setChecked ( bool on )
{
   m_on = on;
   if ( on )
      setPixmap ( skin->getButton ( m_on_n ) );
   else
      setPixmap ( skin->getButton ( m_off_n ) );
}
void ToggleButton::mousePressEvent ( QMouseEvent* )
{
   m_cursorin = true;
   m_old_on = m_on;
   if ( m_on )
      setPixmap ( skin->getButton ( m_off_p ) );
   else
      setPixmap ( skin->getButton ( m_on_p ) );
}

void ToggleButton::mouseReleaseEvent ( QMouseEvent* )
{
   if ( m_cursorin ) {
       m_on = !m_old_on;
       setChecked ( m_on );
       emit clicked( m_on );
   } else {
       m_on = m_old_on;
       setChecked ( m_on );
   }
}

void ToggleButton::mouseMoveEvent (QMouseEvent *e)
{
    if ( !m_cursorin && rect().contains(e->pos()) ) {
        m_cursorin = true;
        if ( m_old_on )
            setPixmap ( skin->getButton ( m_off_p ) );
        else
            setPixmap ( skin->getButton ( m_on_p ) );            
    } else if ( m_cursorin && !rect().contains(e->pos()) ) {
        m_cursorin = false;
        if ( m_old_on )
            setPixmap ( skin->getButton ( m_on_n ) );
        else
            setPixmap ( skin->getButton ( m_off_n ) );
    }
}
2 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> 2009-05-02 18:17:00 +0000 creating file operations plugin structure' href='/qmmp/commit/src/plugins/General/fileops/settingsdialog.cpp?id=2e2a47aa3d46cacf42f776138429fc2855874739'>2e2a47aa3
8618c1e26

2e2a47aa3
08de867fc


8618c1e26








08de867fc
8618c1e26

8618c1e26

086719406





520d57d70

8618c1e26





520d57d70
8618c1e26
520d57d70
8618c1e26




520d57d70
08de867fc
08de867fc

520d57d70
08de867fc
8618c1e26
08de867fc
8618c1e26

08de867fc


520d57d70
08de867fc
8618c1e26
08de867fc
8618c1e26


08de867fc




12fefde04
302e62bd7

12fefde04
08de867fc

8618c1e26











12fefde04
08de867fc
12fefde04
08de867fc

520d57d70
08de867fc
8618c1e26
08de867fc
302e62bd7
08de867fc
7c6e2dd39
8618c1e26
7c6e2dd39

8618c1e26
7c6e2dd39
16cb4ed99
7c6e2dd39
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
                                                                            
                                                                           
                                                                            



                                                                            
                                                                           








                                                                            
                                                                            
                                                                             
                    
                      
                       

                    
                         
                      
                              
                                         
                    
                         




                                               
                       
                                                                                            
                                                                                              
 

                                                                 

                                                              
     
                                       
                                            

                                                                                          
 



                                                                                                        
 



                                                                                                                
 


                                                                                    
 



                                                        
 
            

                        
                                                                                                    
                      
                  








                                                                 
                                   

                               
                  
                                                         
     











                                                                                                     
 



                                                                                                
 
                                                                                             
     



                        
                                           
 

                                           
                                        
                                          
                               
 
                                           
 


                                                                                                         
 

                                                                       





                                                                                       

 
                                              
 

                                                                    
 


                                      








                                                         
     

                                                                                               

                                                                                                                         





                                                                               

                                                              





                                                    
         
                                                                        
         




                                                           
         
     

 
                                                                 
 
                                           
     

                                                                                           


     
                                                                
 
                                           
     


                                                                                           




                                  
                                                                                                     

                                                                
                                                                                   

 











                                                       
                                                       
 
                                  

 
                                            
 
                                                                                                                 
                      
                                           
 
 
                                                                             

                                                                
                                                                                  
                                     
                          
 
/***************************************************************************
 *   Copyright(C) 2009-2021 by Ilya Kotov                                 *
 *   forkotov02@ya.ru                                                      *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU 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 <QSettings>
#include <QHeaderView>
#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QStandardPaths>
#include <qmmp/qmmp.h>
#include <qmmpui/filedialog.h>
#include <qmmpui/metadataformattermenu.h>
#include "fileops.h"
#include "hotkeydialog.h"
#include "settingsdialog.h"

SettingsDialog::SettingsDialog(QWidget *parent)
        : QDialog(parent)
{
    m_ui.setupUi(this);
    m_ui.tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    m_ui.tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("FileOps");
    int i = 0;
    while(!settings.value(QString("name_%1").arg(i)).isNull())
    {
        m_ui.tableWidget->insertRow(i);
        QCheckBox *checkBox = new QCheckBox;
        checkBox->setFocusPolicy(Qt::NoFocus);
        checkBox->setChecked(settings.value(QString("enabled_%1").arg(i), true).toBool());

        QComboBox *comboBox = createComboBox();
        int ci = comboBox->findData(settings.value(QString("action_%1").arg(i), FileOps::COPY).toInt());
        comboBox->setCurrentIndex(qMax(ci, 0));
        connect(comboBox, SIGNAL(activated(int)), SLOT(updateLineEdits()));

        QTableWidgetItem *nameItem = new QTableWidgetItem(settings.value(QString("name_%1").arg(i)).toString());
        nameItem->setData(PatternRole, settings.value(QString("pattern_%1").arg(i)).toString());
        nameItem->setData(DestionationRole, settings.value(QString("destination_%1").arg(i)).toString());
        nameItem->setData(CommandRole, settings.value(QString("command_%1").arg(i)).toString());

        QTableWidgetItem *hotkeyItem = new QTableWidgetItem();
        hotkeyItem->setText(settings.value(QString("hotkey_%1").arg(i)).toString());
        hotkeyItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

        m_ui.tableWidget->setCellWidget(i, 0, checkBox);
        m_ui.tableWidget->setCellWidget(i, 1, comboBox);
        m_ui.tableWidget->setItem(i, 2, nameItem);
        m_ui.tableWidget->setItem(i, 3, hotkeyItem);

        ++i;
    }
    settings.endGroup();
    connect(m_ui.tableWidget, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(updateLineEdits()));
    updateLineEdits();
    createMenus();
}


SettingsDialog::~SettingsDialog()
{}

void SettingsDialog::accept()
{
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("FileOps");
    //remove all previous keys
    settings.remove(QString());
    //save actions
    for(int i = 0; i < m_ui.tableWidget->rowCount(); ++i)
    {
        QTableWidgetItem *item = m_ui.tableWidget->item(i, 2);
        if(item->text().isEmpty())
            continue;

        settings.setValue(QString("name_%1").arg(i), item->text());
        settings.setValue(QString("destination_%1").arg(i), item->data(DestionationRole).toString());

        QCheckBox *checkBox = qobject_cast<QCheckBox *>(m_ui.tableWidget->cellWidget(i, 0));
        settings.setValue(QString("enabled_%1").arg(i), checkBox->isChecked());

        QComboBox *comboBox = qobject_cast<QComboBox *>(m_ui.tableWidget->cellWidget(i, 1));
        settings.setValue(QString("action_%1").arg(i), comboBox->itemData(comboBox->currentIndex()));

        if(comboBox->itemData(comboBox->currentIndex()) == FileOps::EXECUTE)
            settings.setValue(QString("command_%1").arg(i), item->data(CommandRole).toString());
        else
            settings.setValue(QString("pattern_%1").arg(i), item->data(PatternRole).toString());

        settings.setValue(QString("hotkey_%1").arg(i), m_ui.tableWidget->item(i, 3)->text());
    }
    settings.endGroup();
    QDialog::accept();
}

void SettingsDialog::on_newButton_clicked()
{
    int row = m_ui.tableWidget->rowCount();
    m_ui.tableWidget->insertRow(row);
    QCheckBox *checkBox = new QCheckBox;
    checkBox->setFocusPolicy(Qt::NoFocus);
    checkBox->setChecked(true);

    QComboBox *comboBox = createComboBox();

    QTableWidgetItem *nameItem = new QTableWidgetItem(tr("New action"));
    nameItem->setData(DestionationRole, QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
    nameItem->setData(PatternRole, "%p - %t");

    connect(comboBox, SIGNAL(activated(int)), SLOT(updateLineEdits()));

    m_ui.tableWidget->setCellWidget(row, 0, checkBox);
    m_ui.tableWidget->setCellWidget(row, 1, comboBox);
    m_ui.tableWidget->setItem(row, 2, nameItem);
    QTableWidgetItem *hotkeyItem = new QTableWidgetItem();
    m_ui.tableWidget->setItem(row, 3, hotkeyItem);
    m_ui.tableWidget->item(row, 3)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}

void SettingsDialog::on_deleteButton_clicked()
{
    if(m_ui.tableWidget->currentRow() >= 0)
        m_ui.tableWidget->removeRow(m_ui.tableWidget->currentRow());
}

void SettingsDialog::updateLineEdits()
{
    m_ui.destinationEdit->setVisible(false);
    m_ui.destinationLabel->setVisible(false);
    m_ui.destButton->setVisible(false);
    m_ui.patternEdit->setVisible(false);
    m_ui.patternLabel->setVisible(false);
    m_ui.patternButton->setVisible(false);
    m_ui.patternLabel->setText(tr("File name pattern:"));

    if(m_ui.tableWidget->currentRow() >= 0)
    {
        QTableWidgetItem *nameItem = m_ui.tableWidget->item(m_ui.tableWidget->currentRow(), 2);
        m_ui.destinationEdit->setText(nameItem->data(DestionationRole).toString());
        QComboBox *comboBox = qobject_cast<QComboBox *>(m_ui.tableWidget->cellWidget(m_ui.tableWidget->currentRow(), 1));
        int action = comboBox->itemData(comboBox->currentIndex()).toInt();

        if(action == FileOps::EXECUTE)
             m_ui.patternEdit->setText(nameItem->data(CommandRole).toString());
        else
            m_ui.patternEdit->setText(nameItem->data(PatternRole).toString());

        if(action == FileOps::COPY || action == FileOps::MOVE)
        {
            m_ui.destinationEdit->setVisible(true);
            m_ui.destinationLabel->setVisible(true);
            m_ui.destButton->setVisible(true);
            m_ui.patternEdit->setVisible(true);
            m_ui.patternLabel->setVisible(true);
            m_ui.patternButton->setVisible(true);
        }
        else if(action == FileOps::RENAME || action == FileOps::EXECUTE)
        {
            m_ui.patternEdit->setVisible(true);
            m_ui.patternLabel->setVisible(true);
            m_ui.patternButton->setVisible(true);
            if(action == FileOps::EXECUTE)
                m_ui.patternLabel->setText(tr("Command:"));
        }
    }
}

void SettingsDialog::on_destinationEdit_textChanged(QString dest)
{
    if(m_ui.tableWidget->currentRow() >= 0)
    {
        QTableWidgetItem *item = m_ui.tableWidget->item(m_ui.tableWidget->currentRow(), 2);
        item->setData(DestionationRole, dest);
    }
}

void SettingsDialog::on_patternEdit_textChanged(QString pattern)
{
    if(m_ui.tableWidget->currentRow() >= 0)
    {
        QTableWidgetItem *item = m_ui.tableWidget->item(m_ui.tableWidget->currentRow(), 2);
        item->setData(PatternRole, pattern);
        item->setData(CommandRole, pattern);
    }
}

void SettingsDialog::createMenus()
{
    MetaDataFormatterMenu *menu = new MetaDataFormatterMenu(MetaDataFormatterMenu::TITLE_MENU, this);
    m_ui.patternButton->setMenu(menu);
    m_ui.patternButton->setPopupMode(QToolButton::InstantPopup);
    connect(menu, SIGNAL(patternSelected(QString)), SLOT(addTitleString(QString)));
}

QComboBox *SettingsDialog::createComboBox()
{
    QComboBox *comboBox = new QComboBox;
    comboBox->addItem(tr("Copy"), FileOps::COPY);
    comboBox->addItem(tr("Rename"), FileOps::RENAME);
    comboBox->addItem(tr("Move"), FileOps::MOVE);
    comboBox->addItem(tr("Remove"), FileOps::REMOVE);
    comboBox->addItem(tr("Execute"), FileOps::EXECUTE);
    comboBox->setFocusPolicy(Qt::NoFocus);
    return comboBox;
}

void SettingsDialog::addTitleString(const QString &str)
{
    m_ui.patternEdit->insert(str);
}

void SettingsDialog::on_destButton_clicked()
{
    QString dir = FileDialog::getExistingDirectory(this, tr("Choose a directory"), m_ui.destinationEdit->text());
    if(!dir.isEmpty())
        m_ui.destinationEdit->setText(dir);
}

void SettingsDialog::on_tableWidget_itemDoubleClicked(QTableWidgetItem *item)
{
    HotkeyDialog *dialog = new HotkeyDialog(item->text(), this);
    if(m_ui.tableWidget->column(item) == 3 && dialog->exec() == QDialog::Accepted)
        item->setText(dialog->key());
    dialog->deleteLater();
}