Browse Source

Follow project coding style. Issue #2192.

adaptive-webui-19844
Eugene Shalygin 9 years ago
parent
commit
f9c2bd3502
  1. 309
      src/gui/properties/downloadedpiecesbar.cpp
  2. 60
      src/gui/properties/downloadedpiecesbar.h
  3. 3
      src/gui/properties/pieceavailabilitybar.cpp
  4. 2
      src/gui/properties/pieceavailabilitybar.h
  5. 1219
      src/gui/properties/propertieswidget.cpp
  6. 117
      src/gui/properties/propertieswidget.h

309
src/gui/properties/downloadedpiecesbar.cpp

@ -32,212 +32,199 @@
#include <QDebug> #include <QDebug>
#include "downloadedpiecesbar.h" #include "downloadedpiecesbar.h"
DownloadedPiecesBar::DownloadedPiecesBar(QWidget *parent): QWidget(parent) DownloadedPiecesBar::DownloadedPiecesBar(QWidget *parent) : QWidget(parent)
{ {
setToolTip(QString("%1\n%2\n%3").arg(tr("White: Missing pieces")).arg(tr("Green: Partial pieces")).arg(tr("Blue: Completed pieces"))); setToolTip(QString("%1\n%2\n%3").arg(tr("White: Missing pieces")).arg(tr("Green: Partial pieces")).arg(tr("Blue: Completed pieces")));
m_bgColor = 0xffffff; m_bgColor = 0xffffff;
m_borderColor = palette().color(QPalette::Dark).rgb(); m_borderColor = palette().color(QPalette::Dark).rgb();
m_pieceColor = 0x0000ff; m_pieceColor = 0x0000ff;
m_dlPieceColor = 0x00d000; m_dlPieceColor = 0x00d000;
updatePieceColors(); updatePieceColors();
} }
QVector<float> DownloadedPiecesBar::bitfieldToFloatVector(const QBitArray &vecin, int reqSize) QVector<float> DownloadedPiecesBar::bitfieldToFloatVector(const QBitArray &vecin, int reqSize)
{ {
QVector<float> result(reqSize, 0.0); QVector<float> result(reqSize, 0.0);
if (vecin.isEmpty()) return result; if (vecin.isEmpty()) return result;
const float ratio = vecin.size() / (float)reqSize;
// simple linear transformation algorithm
// for example:
// image.x(0) = pieces.x(0.0 >= x < 1.7)
// image.x(1) = pieces.x(1.7 >= x < 3.4)
for (int x = 0; x < reqSize; ++x) {
// R - real
const float fromR = x * ratio;
const float toR = (x + 1) * ratio;
// C - integer
int fromC = fromR;// std::floor not needed
int toC = std::ceil(toR);
if (toC > vecin.size())
--toC;
// position in pieces table
int x2 = fromC;
// little speed up for really big pieces table, 10K+ size
const int toCMinusOne = toC - 1;
// value in returned vector
float value = 0;
// case when calculated range is (15.2 >= x < 15.7)
if (x2 == toCMinusOne) {
if (vecin[x2]) {
value += ratio;
}
++x2;
}
// case when (15.2 >= x < 17.8)
else {
// subcase (15.2 >= x < 16)
if (x2 != fromR) {
if (vecin[x2]) {
value += 1.0 - (fromR - fromC);
}
++x2;
}
// subcase (16 >= x < 17) const float ratio = vecin.size() / (float)reqSize;
for (; x2 < toCMinusOne; ++x2) {
if (vecin[x2]) { // simple linear transformation algorithm
value += 1.0; // for example:
} // image.x(0) = pieces.x(0.0 >= x < 1.7)
} // image.x(1) = pieces.x(1.7 >= x < 3.4)
for (int x = 0; x < reqSize; ++x) {
// R - real
const float fromR = x * ratio;
const float toR = (x + 1) * ratio;
// C - integer
int fromC = fromR;// std::floor not needed
int toC = std::ceil(toR);
if (toC > vecin.size())
--toC;
// position in pieces table
int x2 = fromC;
// little speed up for really big pieces table, 10K+ size
const int toCMinusOne = toC - 1;
// subcase (17 >= x < 17.8) // value in returned vector
if (x2 == toCMinusOne) { float value = 0;
if (vecin[x2]) {
value += 1.0 - (toC - toR); // case when calculated range is (15.2 >= x < 15.7)
if (x2 == toCMinusOne) {
if (vecin[x2])
value += ratio;
++x2;
}
// case when (15.2 >= x < 17.8)
else {
// subcase (15.2 >= x < 16)
if (x2 != fromR) {
if (vecin[x2])
value += 1.0 - (fromR - fromC);
++x2;
}
// subcase (16 >= x < 17)
for (; x2 < toCMinusOne; ++x2)
if (vecin[x2])
value += 1.0;
// subcase (17 >= x < 17.8)
if (x2 == toCMinusOne) {
if (vecin[x2])
value += 1.0 - (toC - toR);
++x2;
}
} }
++x2;
}
}
// normalization <0, 1> // normalization <0, 1>
value /= ratio; value /= ratio;
// float precision sometimes gives > 1, because in not possible to store irrational numbers // float precision sometimes gives > 1, because in not possible to store irrational numbers
value = qMin(value, (float)1.0); value = qMin(value, (float)1.0);
result[x] = value; result[x] = value;
} }
return result; return result;
} }
int DownloadedPiecesBar::mixTwoColors(int &rgb1, int &rgb2, float ratio) int DownloadedPiecesBar::mixTwoColors(int &rgb1, int &rgb2, float ratio)
{ {
int r1 = qRed(rgb1); int r1 = qRed(rgb1);
int g1 = qGreen(rgb1); int g1 = qGreen(rgb1);
int b1 = qBlue(rgb1); int b1 = qBlue(rgb1);
int r2 = qRed(rgb2); int r2 = qRed(rgb2);
int g2 = qGreen(rgb2); int g2 = qGreen(rgb2);
int b2 = qBlue(rgb2); int b2 = qBlue(rgb2);
float ratio_n = 1.0 - ratio; float ratio_n = 1.0 - ratio;
int r = (r1 * ratio_n) + (r2 * ratio); int r = (r1 * ratio_n) + (r2 * ratio);
int g = (g1 * ratio_n) + (g2 * ratio); int g = (g1 * ratio_n) + (g2 * ratio);
int b = (b1 * ratio_n) + (b2 * ratio); int b = (b1 * ratio_n) + (b2 * ratio);
return qRgb(r, g, b); return qRgb(r, g, b);
} }
void DownloadedPiecesBar::updateImage() void DownloadedPiecesBar::updateImage()
{ {
// qDebug() << "updateImage"; // qDebug() << "updateImage";
QImage image2(width() - 2, 1, QImage::Format_RGB888); QImage image2(width() - 2, 1, QImage::Format_RGB888);
if (image2.isNull()) { if (image2.isNull()) {
qDebug() << "QImage image2() allocation failed, width():" << width(); qDebug() << "QImage image2() allocation failed, width():" << width();
return; return;
} }
if (m_pieces.isEmpty()) { if (m_pieces.isEmpty()) {
image2.fill(0xffffff); image2.fill(0xffffff);
m_image = image2; m_image = image2;
update(); update();
return; return;
}
QVector<float> scaled_pieces = bitfieldToFloatVector(m_pieces, image2.width());
QVector<float> scaled_pieces_dl = bitfieldToFloatVector(m_downloadedPieces, image2.width());
// filling image
for (int x = 0; x < scaled_pieces.size(); ++x)
{
float pieces2_val = scaled_pieces.at(x);
float pieces2_val_dl = scaled_pieces_dl.at(x);
if (pieces2_val_dl != 0)
{
float fill_ratio = pieces2_val + pieces2_val_dl;
float ratio = pieces2_val_dl / fill_ratio;
int mixedColor = mixTwoColors(m_pieceColor, m_dlPieceColor, ratio);
mixedColor = mixTwoColors(m_bgColor, mixedColor, fill_ratio);
image2.setPixel(x, 0, mixedColor);
} }
else
{ QVector<float> scaled_pieces = bitfieldToFloatVector(m_pieces, image2.width());
image2.setPixel(x, 0, m_pieceColors[pieces2_val * 255]); QVector<float> scaled_pieces_dl = bitfieldToFloatVector(m_downloadedPieces, image2.width());
// filling image
for (int x = 0; x < scaled_pieces.size(); ++x) {
float pieces2_val = scaled_pieces.at(x);
float pieces2_val_dl = scaled_pieces_dl.at(x);
if (pieces2_val_dl != 0) {
float fill_ratio = pieces2_val + pieces2_val_dl;
float ratio = pieces2_val_dl / fill_ratio;
int mixedColor = mixTwoColors(m_pieceColor, m_dlPieceColor, ratio);
mixedColor = mixTwoColors(m_bgColor, mixedColor, fill_ratio);
image2.setPixel(x, 0, mixedColor);
}
else {
image2.setPixel(x, 0, m_pieceColors[pieces2_val * 255]);
}
} }
} m_image = image2;
m_image = image2;
} }
void DownloadedPiecesBar::setProgress(const QBitArray &pieces, const QBitArray &downloadedPieces) void DownloadedPiecesBar::setProgress(const QBitArray &pieces, const QBitArray &downloadedPieces)
{ {
m_pieces = pieces; m_pieces = pieces;
m_downloadedPieces = downloadedPieces; m_downloadedPieces = downloadedPieces;
updateImage(); updateImage();
update(); update();
} }
void DownloadedPiecesBar::updatePieceColors() void DownloadedPiecesBar::updatePieceColors()
{ {
m_pieceColors = QVector<int>(256); m_pieceColors = QVector<int>(256);
for (int i = 0; i < 256; ++i) { for (int i = 0; i < 256; ++i) {
float ratio = (i / 255.0); float ratio = (i / 255.0);
m_pieceColors[i] = mixTwoColors(m_bgColor, m_pieceColor, ratio); m_pieceColors[i] = mixTwoColors(m_bgColor, m_pieceColor, ratio);
} }
} }
void DownloadedPiecesBar::clear() void DownloadedPiecesBar::clear()
{ {
m_image = QImage(); m_image = QImage();
update(); update();
} }
void DownloadedPiecesBar::paintEvent(QPaintEvent *) void DownloadedPiecesBar::paintEvent(QPaintEvent*)
{ {
QPainter painter(this); QPainter painter(this);
QRect imageRect(1, 1, width() - 2, height() - 2); QRect imageRect(1, 1, width() - 2, height() - 2);
if (m_image.isNull()) if (m_image.isNull()) {
{ painter.setBrush(Qt::white);
painter.setBrush(Qt::white); painter.drawRect(imageRect);
painter.drawRect(imageRect); }
} else {
else if (m_image.width() != imageRect.width())
{ updateImage();
if (m_image.width() != imageRect.width()) painter.drawImage(imageRect, m_image);
updateImage(); }
painter.drawImage(imageRect, m_image); QPainterPath border;
} border.addRect(0, 0, width() - 1, height() - 1);
QPainterPath border;
border.addRect(0, 0, width() - 1, height() - 1); painter.setPen(m_borderColor);
painter.drawPath(border);
painter.setPen(m_borderColor);
painter.drawPath(border);
} }
void DownloadedPiecesBar::setColors(int background, int border, int complete, int incomplete) void DownloadedPiecesBar::setColors(int background, int border, int complete, int incomplete)
{ {
m_bgColor = background; m_bgColor = background;
m_borderColor = border; m_borderColor = border;
m_pieceColor = complete; m_pieceColor = complete;
m_dlPieceColor = incomplete; m_dlPieceColor = incomplete;
updatePieceColors();
updateImage();
update();
}
updatePieceColors();
updateImage();
update();
}

60
src/gui/properties/downloadedpiecesbar.h

@ -38,48 +38,48 @@
#include <QVector> #include <QVector>
class DownloadedPiecesBar: public QWidget { class DownloadedPiecesBar: public QWidget {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(DownloadedPiecesBar) Q_DISABLE_COPY(DownloadedPiecesBar)
private: private:
QImage m_image; QImage m_image;
// I used values, because it should be possible to change colors in runtime // I used values, because it should be possible to change colors in runtime
// background color // background color
int m_bgColor; int m_bgColor;
// border color // border color
int m_borderColor; int m_borderColor;
// complete piece color // complete piece color
int m_pieceColor; int m_pieceColor;
// incomplete piece color // incomplete piece color
int m_dlPieceColor; int m_dlPieceColor;
// buffered 256 levels gradient from bg_color to piece_color // buffered 256 levels gradient from bg_color to piece_color
QVector<int> m_pieceColors; QVector<int> m_pieceColors;
// last used bitfields, uses to better resize redraw // last used bitfields, uses to better resize redraw
// TODO: make a diff pieces to new pieces and update only changed pixels, speedup when update > 20x faster // TODO: make a diff pieces to new pieces and update only changed pixels, speedup when update > 20x faster
QBitArray m_pieces; QBitArray m_pieces;
QBitArray m_downloadedPieces; QBitArray m_downloadedPieces;
// scale bitfield vector to float vector // scale bitfield vector to float vector
QVector<float> bitfieldToFloatVector(const QBitArray &vecin, int reqSize); QVector<float> bitfieldToFloatVector(const QBitArray &vecin, int reqSize);
// mix two colors by light model, ratio <0, 1> // mix two colors by light model, ratio <0, 1>
int mixTwoColors(int &rgb1, int &rgb2, float ratio); int mixTwoColors(int &rgb1, int &rgb2, float ratio);
// draw new image and replace actual image // draw new image and replace actual image
void updateImage(); void updateImage();
public: public:
DownloadedPiecesBar(QWidget *parent); DownloadedPiecesBar(QWidget *parent);
void setProgress(const QBitArray &m_pieces, const QBitArray &downloadedPieces); void setProgress(const QBitArray &m_pieces, const QBitArray &downloadedPieces);
void updatePieceColors(); void updatePieceColors();
void clear(); void clear();
void setColors(int background, int border, int complete, int incomplete); void setColors(int background, int border, int complete, int incomplete);
protected: protected:
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *);
}; };
#endif // DOWNLOADEDPIECESBAR_H #endif // DOWNLOADEDPIECESBAR_H

3
src/gui/properties/pieceavailabilitybar.cpp

@ -32,7 +32,6 @@
#include <QDebug> #include <QDebug>
#include "pieceavailabilitybar.h" #include "pieceavailabilitybar.h"
PieceAvailabilityBar::PieceAvailabilityBar(QWidget *parent) PieceAvailabilityBar::PieceAvailabilityBar(QWidget *parent)
: QWidget(parent) : QWidget(parent)
{ {
@ -192,7 +191,7 @@ void PieceAvailabilityBar::clear()
update(); update();
} }
void PieceAvailabilityBar::paintEvent(QPaintEvent *) void PieceAvailabilityBar::paintEvent(QPaintEvent*)
{ {
QPainter painter(this); QPainter painter(this);
QRect imageRect(1, 1, width() - 2, height() - 2); QRect imageRect(1, 1, width() - 2, height() - 2);

2
src/gui/properties/pieceavailabilitybar.h

@ -76,7 +76,7 @@ public:
void setColors(int background, int border, int available); void setColors(int background, int border, int available);
protected: protected:
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent*);
}; };
#endif // PIECEAVAILABILITYBAR_H #endif // PIECEAVAILABILITYBAR_H

1219
src/gui/properties/propertieswidget.cpp

File diff suppressed because it is too large Load Diff

117
src/gui/properties/propertieswidget.h

@ -55,80 +55,81 @@ class QAction;
class QTimer; class QTimer;
QT_END_NAMESPACE QT_END_NAMESPACE
class PropertiesWidget : public QWidget, private Ui::PropertiesWidget { class PropertiesWidget: public QWidget, private Ui::PropertiesWidget
Q_OBJECT {
Q_DISABLE_COPY(PropertiesWidget) Q_OBJECT
Q_DISABLE_COPY(PropertiesWidget)
public: public:
enum SlideState {REDUCED, VISIBLE}; enum SlideState {REDUCED, VISIBLE};
public: public:
PropertiesWidget(QWidget *parent, MainWindow* main_window, TransferListWidget *transferList); PropertiesWidget(QWidget *parent, MainWindow *main_window, TransferListWidget *transferList);
~PropertiesWidget(); ~PropertiesWidget();
BitTorrent::TorrentHandle *getCurrentTorrent() const; BitTorrent::TorrentHandle *getCurrentTorrent() const;
TrackerList* getTrackerList() const { return trackerList; } TrackerList *getTrackerList() const { return trackerList; }
PeerListWidget* getPeerList() const { return peersList; } PeerListWidget *getPeerList() const { return peersList; }
QTreeView* getFilesList() const { return filesList; } QTreeView *getFilesList() const { return filesList; }
SpeedWidget* getSpeedWidget() const { return speedWidget; } SpeedWidget *getSpeedWidget() const { return speedWidget; }
protected: protected:
QPushButton* getButtonFromIndex(int index); QPushButton *getButtonFromIndex(int index);
bool applyPriorities(); bool applyPriorities();
protected slots: protected slots:
void loadTorrentInfos(BitTorrent::TorrentHandle *const torrent); void loadTorrentInfos(BitTorrent::TorrentHandle *const torrent);
void updateTorrentInfos(BitTorrent::TorrentHandle *const torrent); void updateTorrentInfos(BitTorrent::TorrentHandle *const torrent);
void loadUrlSeeds(); void loadUrlSeeds();
void askWebSeed(); void askWebSeed();
void deleteSelectedUrlSeeds(); void deleteSelectedUrlSeeds();
void copySelectedWebSeedsToClipboard() const; void copySelectedWebSeedsToClipboard() const;
void editWebSeed(); void editWebSeed();
void displayFilesListMenu(const QPoint& pos); void displayFilesListMenu(const QPoint &pos);
void displayWebSeedListMenu(const QPoint& pos); void displayWebSeedListMenu(const QPoint &pos);
void filteredFilesChanged(); void filteredFilesChanged();
void showPiecesDownloaded(bool show); void showPiecesDownloaded(bool show);
void showPiecesAvailability(bool show); void showPiecesAvailability(bool show);
void renameSelectedFile(); void renameSelectedFile();
void openSelectedFile(); void openSelectedFile();
public slots: public slots:
void setVisibility(bool visible); void setVisibility(bool visible);
void loadDynamicData(); void loadDynamicData();
void clear(); void clear();
void readSettings(); void readSettings();
void saveSettings(); void saveSettings();
void reloadPreferences(); void reloadPreferences();
void openDoubleClickedFile(const QModelIndex &); void openDoubleClickedFile(const QModelIndex &);
void loadTrackers(BitTorrent::TorrentHandle *const torrent); void loadTrackers(BitTorrent::TorrentHandle *const torrent);
private: private:
void openFile(const QModelIndex &index); void openFile(const QModelIndex &index);
void openFolder(const QModelIndex &index, bool containing_folder); void openFolder(const QModelIndex &index, bool containing_folder);
private: private:
TransferListWidget *transferList; TransferListWidget *transferList;
MainWindow *main_window; MainWindow *main_window;
BitTorrent::TorrentHandle *m_torrent; BitTorrent::TorrentHandle *m_torrent;
QTimer *refreshTimer; QTimer *refreshTimer;
SlideState state; SlideState state;
TorrentContentFilterModel *PropListModel; TorrentContentFilterModel *PropListModel;
PropListDelegate *PropDelegate; PropListDelegate *PropDelegate;
PeerListWidget *peersList; PeerListWidget *peersList;
TrackerList *trackerList; TrackerList *trackerList;
SpeedWidget *speedWidget; SpeedWidget *speedWidget;
QList<int> slideSizes; QList<int> slideSizes;
DownloadedPiecesBar *downloaded_pieces; DownloadedPiecesBar *downloaded_pieces;
PieceAvailabilityBar *pieces_availability; PieceAvailabilityBar *pieces_availability;
PropTabBar *m_tabBar; PropTabBar *m_tabBar;
LineEdit *m_contentFilterLine; LineEdit *m_contentFilterLine;
QShortcut *editHotkeyFile; QShortcut *editHotkeyFile;
QShortcut *editHotkeyWeb; QShortcut *editHotkeyWeb;
QShortcut *deleteHotkeyWeb; QShortcut *deleteHotkeyWeb;
QShortcut *openHotkeyFile; QShortcut *openHotkeyFile;
private slots: private slots:
void filterText(const QString& filter); void filterText(const QString &filter);
void updateSavePath(BitTorrent::TorrentHandle *const torrent); void updateSavePath(BitTorrent::TorrentHandle *const torrent);
}; };
#endif // PROPERTIESWIDGET_H #endif // PROPERTIESWIDGET_H

Loading…
Cancel
Save