source: src-qt4/pc-zmanager/dialogpartition.cpp @ 125dbc9

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 125dbc9 was 125dbc9, checked in by Kris Moore <kris@…>, 11 months ago

Welcome the new 'pc-zmanager' for ZFS & Disk Management

A HUGE Thank You to: Claudio L <claudio@…>

Update pkg-plist and add it to the control panel

  • Property mode set to 100644
File size: 9.2 KB
Line 
1#include "dialogpartition.h"
2#include "ui_dialogpartition.h"
3#include <QTreeWidget>
4#include <QTreeWidgetItem>
5#include <QGraphicsWidget>
6#include <QGraphicsRectItem>
7#include <QTransform>
8#include <QPen>
9#include <QBrush>
10
11
12DialogPartition::DialogPartition(QWidget *parent) :
13    QDialog(parent),
14    ui(new Ui::DialogPartition)
15{
16    ui->setupUi(this);
17    device=NULL;
18
19    ui->devLayout->header()->setStretchLastSection(false);
20    ui->devLayout->header()->setResizeMode(0,QHeaderView::Stretch);
21    ui->devLayout->header()->setResizeMode(1,QHeaderView::ResizeToContents);
22    ui->devLayout->header()->setResizeMode(2,QHeaderView::ResizeToContents);
23    ui->devLayout->header()->setResizeMode(3,QHeaderView::ResizeToContents);
24    ui->devLayout->header()->setResizeMode(4,QHeaderView::ResizeToContents);
25    ui->devLayout->setIconSize(QSize(22,22));
26    ui->devLayout->setSelectionMode(QAbstractItemView::SingleSelection);
27
28    ui->GraphicPart->setScene(new QGraphicsScene());
29
30}
31
32DialogPartition::~DialogPartition()
33{
34    delete ui;
35}
36
37
38void DialogPartition::setDevice(vdev_t * _device)
39{
40    QString tmp;
41    device=_device;
42
43    ui->devName->setText(device->Name);
44    ui->devTotalSize->setText(printBytes(device->Size));
45
46
47    /* This is to overcome an implementation limitation in Qt */
48    /* The slider widget can only count up to 2^32 in a 32 bit system */
49    /* So we will use coarser granularity to (larger sectors) to make it work */
50    granularity=0;
51    unsigned long long temp=device->SectorsCount;
52    while(temp > 1ULL<<31) {
53        granularity++;
54        temp>>=1;
55    }
56
57    // SHOW ONLY PARTITIONS THAT ARE AVAILABLE DEPENDING ON TYPE OF TABLE
58
59    ui->PartType->clear();
60    if(device->PartType=="MBR") {
61        // TODO: ADD ALL PARTITION TYPES FROM GPART SOURCE CODE
62        ui->PartType->addItem("freebsd",QVariant(1));
63        ui->PartType->setCurrentIndex(0);
64    }
65    if(device->PartType=="GPT") {
66        // TODO: ADD ALL PARTITION TYPES FROM GPART SOURCE CODE
67        ui->PartType->addItem("freebsd-ufs",QVariant(1));
68        ui->PartType->addItem("freebsd-zfs",QVariant(1));
69        ui->PartType->addItem("freebsd-boot",QVariant(1));
70        ui->PartType->addItem("freebsd-swap",QVariant(1));
71        ui->PartType->setCurrentIndex(0);
72    }
73    if(device->PartType=="BSD") {
74        // BSD PARTITION ONLY SUPPORTS FreeBSD types
75        ui->PartType->addItem("freebsd-ufs",QVariant(1));
76        ui->PartType->addItem("freebsd-zfs",QVariant(1));
77        ui->PartType->addItem("freebsd-swap",QVariant(1));
78        ui->PartType->setCurrentIndex(0);
79    }
80
81
82
83    QList<vdev_t>::const_iterator idx=device->Partitions.constBegin();
84    unsigned long long sector=((device->Level==0)? device->SectorStart:0),maxsectorcount=0;
85    int maxspaceidx=-1,numidx=0;
86    QTreeWidgetItem *maxspaceitem=NULL;
87
88    int coloridx=0;
89    double scalex=(double)1000.0/(double)device->SectorsCount;
90
91
92
93    ui->GraphicPart->scene()->clear();
94
95    while(idx!=device->Partitions.constEnd())
96    {
97        if((*idx).SectorStart>sector) {
98            QTreeWidgetItem *item=new QTreeWidgetItem(ui->devLayout);
99            item->setIcon(0,QIcon(":/icons/task-complete.png"));
100            item->setText(0,tr("** FREE **"));
101            item->setText(1,tmp.sprintf("%llu",sector));
102            item->setText(2,tmp.sprintf("%llu",(*idx).SectorStart-sector));
103            item->setText(3,printBytes(((*idx).SectorStart-sector)*device->SectorSize));
104            //item->setDisabled(false);
105            item->setBackgroundColor(4,QColor::fromHsv(coloridx,0,192));
106
107            ui->GraphicPart->scene()->addRect(sector*scalex,0,((*idx).SectorStart-sector)*scalex,100,QPen(QColor::fromHsv(coloridx,0,192)),QBrush(QColor::fromHsv(coloridx,0,192)));
108            coloridx+=30;
109
110            if((*idx).SectorStart-sector>maxsectorcount) { maxspaceitem=item; maxsectorcount=(*idx).SectorStart-sector; maxspaceidx=numidx; }
111            sector=(*idx).SectorStart;
112        }
113
114        QTreeWidgetItem *item=new QTreeWidgetItem(ui->devLayout);
115        item->setIcon(0,QIcon(":/icons/kdf.png"));
116        item->setText(0,(*idx).Name + " [" + (*idx).PartType + "]");
117        item->setText(1,tmp.sprintf("%llu",(*idx).SectorStart));
118        item->setText(2,tmp.sprintf("%llu",(*idx).SectorsCount));
119        item->setText(3,printBytes(((*idx).SectorsCount)*device->SectorSize));
120        //item->setDisabled(true);
121        item->setBackgroundColor(4,QColor::fromHsv(coloridx,128,255));
122        sector=(*idx).SectorStart+(*idx).SectorsCount;
123
124        ui->GraphicPart->scene()->addRect((*idx).SectorStart*scalex,0,(*idx).SectorsCount*scalex,100,QPen(QColor::fromHsv(coloridx,128,255)),QBrush(QColor::fromHsv(coloridx,128,255)));
125        coloridx+=30;
126
127        ++idx;
128        ++numidx;
129
130
131
132    }
133
134
135
136
137    if( ((device->Level==0)? device->SectorStart:0)+device->SectorsCount>sector) {
138        QTreeWidgetItem *item=new QTreeWidgetItem(ui->devLayout);
139        item->setIcon(0,QIcon(":/icons/task-complete.png"));
140        item->setText(0,tr("** FREE **"));
141        item->setText(1,tmp.sprintf("%llu",sector));
142        item->setText(2,tmp.sprintf("%llu",((device->Level==0)? device->SectorStart:0)+device->SectorsCount-sector));
143        item->setText(3,printBytes((((device->Level==0)? device->SectorStart:0)+device->SectorsCount-sector)*device->SectorSize));
144        //item->setDisabled(false);
145        item->setBackgroundColor(4,QColor::fromHsv(coloridx,0,192));
146        if(((device->Level==0)? device->SectorStart:0)+device->SectorsCount-sector>maxsectorcount) { maxspaceitem=item; maxsectorcount=((device->Level==0)? device->SectorStart:0)+device->SectorsCount-sector; maxspaceidx=numidx; }
147
148        ui->GraphicPart->scene()->addRect(sector*scalex,0,(((device->Level==0)? device->SectorStart:0)+device->SectorsCount-sector)*scalex,100,QPen(QColor::fromHsv(coloridx,0,192)),QBrush(QColor::fromHsv(coloridx,0,192)));
149        coloridx+=30;
150
151
152    }
153
154
155    if(!maxspaceitem) {
156        // THIS DISK DOESN'T HAVE ANY FREE SPACE!
157        ui->devLargestFree->setText(tr("No free space!"));
158        ui->SizeSelect->setDisabled(true);
159        ui->sizeSlider->setRange(0,0);
160        ui->sizeSlider->setDisabled(true);
161        ui->SizeText->setDisabled(true);
162        ui->SizeText->clear();
163        ui->PartType->clear();
164        ui->PartType->setEnabled(false);
165    }
166    else {
167
168    ui->devLayout->setCurrentItem(maxspaceitem);
169
170    ui->devLargestFree->setText(printBytes(maxsectorcount*device->SectorSize));
171    }
172
173
174}
175
176
177void DialogPartition::on_SizeSelect_currentIndexChanged(int index)
178{
179    Q_UNUSED(index);
180    ui->SizeText->setText(printBytes(((unsigned long long)ui->sizeSlider->sliderPosition())*device->SectorSize,ui->SizeSelect->currentIndex()));
181}
182
183void DialogPartition::on_sizeSlider_valueChanged(int value)
184{
185    Q_UNUSED(value);
186    ui->SizeText->setText(printBytes(( ((unsigned long long)ui->sizeSlider->sliderPosition())<<granularity)*device->SectorSize,ui->SizeSelect->currentIndex()));
187
188}
189
190
191void DialogPartition::resizeEvent(QResizeEvent *event)
192{
193    QDialog::resizeEvent(event);
194
195    QSize sz=ui->GraphicPart->size();
196    QTransform matrix;
197
198    matrix.scale((qreal)(sz.width()-10)/1000.0,(qreal)(sz.height()-10)/100.0);
199    ui->GraphicPart->setTransform(matrix);
200}
201
202
203void DialogPartition::showEvent(QShowEvent *event)
204{
205    QDialog::showEvent(event);
206
207    QSize sz=ui->GraphicPart->size();
208    QTransform matrix;
209
210    matrix.scale((qreal)(sz.width()-10)/1000.0,(qreal)(sz.height()-10)/100.0);
211    ui->GraphicPart->setTransform(matrix);
212
213}
214
215long long DialogPartition::getStartSector()
216{
217    if(ui->devLayout->currentItem()) {
218        if(ui->devLayout->currentItem()->text(0)==tr("** FREE **")) {
219        long long sector=ui->devLayout->currentItem()->text(1).toLongLong();
220        return sector;
221        }
222
223    }
224    return -1;
225}
226
227unsigned long long DialogPartition::getSectorCount()
228{
229    return ((unsigned long long)ui->sizeSlider->sliderPosition())<<granularity;
230}
231
232QString DialogPartition::getPartType()
233{
234    return ui->PartType->currentText();
235}
236
237
238void DialogPartition::on_devLayout_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
239{
240    Q_UNUSED(previous);
241
242    if(current->text(0)!=tr("** FREE **")) {
243        ui->PartType->setEnabled(false);
244        ui->SizeSelect->setEnabled(false);
245        ui->SizeText->clear();
246        ui->SizeText->setEnabled(false);
247        ui->sizeSlider->setRange(0,0);
248        ui->sizeSlider->setEnabled(false);
249        return;
250    }
251
252    ui->PartType->setEnabled(true);
253    ui->SizeSelect->setEnabled(true);
254    ui->SizeSelect->setEnabled(true);
255    ui->SizeSelect->setCurrentIndex(printUnits(current->text(2).toULongLong()*device->SectorSize));
256    ui->sizeSlider->setRange(0,current->text(2).toULongLong()>>granularity);
257    ui->sizeSlider->setSliderPosition(current->text(2).toULongLong()>>granularity);
258    ui->SizeText->setText(printBytes(current->text(2).toULongLong()*device->SectorSize,ui->SizeSelect->currentIndex()));
259    ui->SizeText->setEnabled(true);
260    return;
261
262}
263
264void DialogPartition::on_PartType_currentIndexChanged(int index)
265{
266    Q_UNUSED(index);
267
268    if((ui->PartType->currentText()=="freebsd-ufs")
269      || (ui->PartType->currentText()=="freebsd-boot") ) ui->newfsCheck->setEnabled(true);
270    else { ui->newfsCheck->setEnabled(false); ui->newfsCheck->setChecked(false); }
271
272}
273
274
275bool DialogPartition::isnewfsChecked()
276{
277 return ui->newfsCheck->isChecked();
278}
Note: See TracBrowser for help on using the repository browser.