almost ready to merge into master
[avrdudequi:avrdudequi.git] / programmerdataview.cpp
1 #include "programmerdataview.h"
2 #include <QLineEdit>
3 #include <QDebug>
4 #include <QApplication>
5
6 ProgrammerDataView *ProgrammerDataView::m_instance = 0;
7
8
9 ProgrammerDataView::ProgrammerDataView() :
10     QObject(0), dataLayout(0), thisWidget(0)
11 {
12
13     programmerDevice = Programmer::instance();
14     //connect(programmerDevice, SIGNAL(programmerChanged(QString)), SLOT(viewData(QString)));
15 }
16
17 QSize ProgrammerDataView::parentSize()
18 {
19     return parentWidget->sizeHint();
20 }
21
22 void ProgrammerDataView::viewData(const QString &id)
23 {
24     currentId = id;
25
26
27     if (thisWidget != 0) {
28         container->removeWidget(thisWidget);
29         delete thisWidget;
30         thisWidget = 0;
31     }
32
33     if (!programmerDevice->isValid()) {
34         if (!currentId.isEmpty())
35             viewDeviceNotFound();
36         return;
37     }
38
39     thisWidget = new QWidget(parentWidget);
40
41     if (id == "avrispmkii")
42         viewAvrIspMkii();
43     else
44         viewTtyDevice();
45     qApp->processEvents();
46 }
47
48 void ProgrammerDataView::viewTtyDevice()
49 {
50
51     labelProgrammerName = new QLabel("Prorgammer Name:", thisWidget);
52     labelProgrammerNameData = new QLabel(thisWidget);
53     labelDevice = new QLabel("Device Name:", thisWidget);
54     labelDeviceData = new QLabel(thisWidget);
55     QLabel *labelBaudrate = new QLabel("Baudrate:", thisWidget);
56     QLabel *labelBaudrateData = new QLabel(thisWidget);
57     QMap<QString, QString> *dataProg = programmerDevice->dataMap();
58     UdevQt::Device cDev= programmerDevice->currentDevice();
59     labelProgrammerNameData->setText(cDev.deviceProperty("ID_MODEL"));
60     labelDeviceData->setText(cDev.name());
61     labelBaudrateData->setText(dataProg->value("device.baudrate"));
62
63     QHBoxLayout *programmerNameLayout = new QHBoxLayout;
64     QHBoxLayout *deviceLayout = new QHBoxLayout;
65     QHBoxLayout *baudrateLayout = new QHBoxLayout;
66
67     programmerNameLayout->addWidget(labelProgrammerName);
68     programmerNameLayout->addWidget(labelProgrammerNameData);
69     deviceLayout->addWidget(labelDevice);
70     deviceLayout->addWidget(labelDeviceData);
71     baudrateLayout->addWidget(labelBaudrate);
72     baudrateLayout->addWidget(labelBaudrateData);
73
74     dataLayout = new QVBoxLayout(thisWidget);
75     dataLayout->addLayout(programmerNameLayout);
76     dataLayout->addLayout(deviceLayout);
77     dataLayout->addLayout(baudrateLayout);
78     container->addWidget(thisWidget);
79
80 }
81
82 void ProgrammerDataView::viewAvrIspMkii()
83 {
84     labelProgrammerName = new QLabel("Prorgammer Name:");
85     labelProgrammerNameData = new QLabel;
86     labelDevice = new QLabel("Device Name:");
87     labelDeviceData = new QLabel;
88     QLabel *labelBitclock = new QLabel("Bitclock(Hz):");
89     QLineEdit *leditBitclock = new QLineEdit;
90     QMap<QString, QString> *dataProg = programmerDevice->dataMap();
91     UdevQt::Device cDev= programmerDevice->currentDevice();
92     labelProgrammerNameData->setText(cDev.deviceProperty("ID_MODEL"));
93     labelDeviceData->setText(cDev.deviceProperty("DEVNAME"));
94
95     leditBitclock->setText(dataProg->value("opt.bitclock"));
96     slotEditLine(leditBitclock->text());
97     QString doDevName;
98     doDevName = "usb:" + cDev.deviceProperty("ID_SERIAL_SHORT").right(4).insert(2, ":");
99     dataProg->insert("device.device_file", doDevName);
100
101     connect(leditBitclock, SIGNAL(textChanged(QString)), this, SLOT(slotEditLine(QString)));
102
103     QHBoxLayout *programmerNameLayout = new QHBoxLayout;
104     QHBoxLayout *deviceLayout = new QHBoxLayout;
105     QHBoxLayout *bitclockLayout = new QHBoxLayout;
106
107     programmerNameLayout->addWidget(labelProgrammerName);
108     programmerNameLayout->addWidget(labelProgrammerNameData);
109     deviceLayout->addWidget(labelDevice);
110     deviceLayout->addWidget(labelDeviceData);
111     bitclockLayout->addWidget(labelBitclock);
112     bitclockLayout->addWidget(leditBitclock);
113
114     dataLayout = new QVBoxLayout;
115     dataLayout->addLayout(programmerNameLayout);
116     dataLayout->addLayout(deviceLayout);
117     dataLayout->addLayout(bitclockLayout);
118
119     thisWidget->setLayout(dataLayout);
120     container->addWidget(thisWidget);
121
122 }
123
124 void ProgrammerDataView::slotEditLine(const QString &s)
125 {
126     QMap<QString, QString> *dataProg = programmerDevice->dataMap();
127     if (currentId == "avrispmkii") {
128         ulong f_target = s.toULong();
129         QString periods;
130         // calcola il periodo e crea una stringa
131         periods.setNum((1.0 / f_target) * 4 * 1000000);
132
133         dataProg->insert("opt.bitclock.period", periods);
134         // aggiorna prog_data bitclock
135         dataProg->insert("opt.bitclock", s);
136     }
137 }
138
139 void ProgrammerDataView::viewDeviceNotFound()
140 {
141     thisWidget = new QWidget;
142     dataLayout = new QVBoxLayout;
143     QLabel *deviceNotFoundMessage = new QLabel("Device not found");
144     dataLayout->addWidget(deviceNotFoundMessage);
145
146     thisWidget->setLayout(dataLayout);
147     container->addWidget(thisWidget);
148     qApp->processEvents();
149
150
151
152 }
153