2015-06-05 00:30:24 +08:00
|
|
|
/*
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre 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 3 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 COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "genericchatitemlayout.h"
|
|
|
|
#include "genericchatitemwidget.h"
|
|
|
|
#include <QBoxLayout>
|
2015-06-19 00:01:30 +08:00
|
|
|
#include <QCollator>
|
2015-06-05 00:30:24 +08:00
|
|
|
#include <cassert>
|
|
|
|
|
2015-06-06 00:57:05 +08:00
|
|
|
// As this layout sorts widget, extra care must be taken when inserting widgets.
|
|
|
|
// Prefer using the build in add and remove functions for modifying widgets.
|
|
|
|
// Inserting widgets other ways would cause this layout to be unable to sort.
|
|
|
|
// As such, they are protected using asserts.
|
|
|
|
|
2015-06-05 00:30:24 +08:00
|
|
|
GenericChatItemLayout::GenericChatItemLayout()
|
|
|
|
: layout(new QVBoxLayout())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericChatItemLayout::~GenericChatItemLayout()
|
|
|
|
{
|
|
|
|
delete layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatItemLayout::addSortedWidget(GenericChatItemWidget* widget, int stretch, Qt::Alignment alignment)
|
|
|
|
{
|
|
|
|
int closest = indexOfClosestSortedWidget(widget);
|
|
|
|
layout->insertWidget(closest, widget, stretch, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GenericChatItemLayout::indexOfSortedWidget(GenericChatItemWidget* widget) const
|
|
|
|
{
|
2016-07-31 22:17:11 +08:00
|
|
|
if (layout->isEmpty())
|
2015-06-05 00:30:24 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
int index = indexOfClosestSortedWidget(widget);
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-05 00:30:24 +08:00
|
|
|
if (index >= layout->count())
|
|
|
|
return -1;
|
|
|
|
|
2016-09-23 10:06:20 +08:00
|
|
|
GenericChatItemWidget* atMid = qobject_cast<GenericChatItemWidget*>(layout->itemAt(index)->widget());
|
2015-06-05 00:30:24 +08:00
|
|
|
assert(atMid != nullptr);
|
|
|
|
|
|
|
|
if (atMid == widget)
|
|
|
|
return index;
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-05 00:30:24 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GenericChatItemLayout::existsSortedWidget(GenericChatItemWidget* widget) const
|
|
|
|
{
|
|
|
|
return indexOfSortedWidget(widget) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericChatItemLayout::removeSortedWidget(GenericChatItemWidget* widget)
|
|
|
|
{
|
|
|
|
if (layout->isEmpty())
|
|
|
|
return;
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-05 00:30:24 +08:00
|
|
|
int index = indexOfClosestSortedWidget(widget);
|
|
|
|
|
|
|
|
if (layout->itemAt(index) == nullptr)
|
|
|
|
return;
|
|
|
|
|
2016-09-23 10:06:20 +08:00
|
|
|
GenericChatItemWidget* atMid = qobject_cast<GenericChatItemWidget*>(layout->itemAt(index)->widget());
|
2015-06-05 00:30:24 +08:00
|
|
|
assert(atMid != nullptr);
|
|
|
|
|
|
|
|
if (atMid == widget)
|
|
|
|
layout->removeWidget(widget);
|
|
|
|
}
|
|
|
|
|
2015-06-06 00:57:05 +08:00
|
|
|
void GenericChatItemLayout::search(const QString &searchString, bool hideAll)
|
|
|
|
{
|
|
|
|
for (int index = 0; index < layout->count(); ++index)
|
|
|
|
{
|
2016-09-23 10:06:20 +08:00
|
|
|
GenericChatItemWidget* widgetAt = qobject_cast<GenericChatItemWidget*>(layout->itemAt(index)->widget());
|
2015-06-06 00:57:05 +08:00
|
|
|
assert(widgetAt != nullptr);
|
|
|
|
|
2015-06-07 11:20:06 +08:00
|
|
|
widgetAt->searchName(searchString, hideAll);
|
2015-06-06 00:57:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 00:30:24 +08:00
|
|
|
QLayout* GenericChatItemLayout::getLayout() const
|
|
|
|
{
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GenericChatItemLayout::indexOfClosestSortedWidget(GenericChatItemWidget* widget) const
|
|
|
|
{
|
|
|
|
// Binary search: Deferred test of equality.
|
2016-04-19 20:06:06 +08:00
|
|
|
int min = 0, max = layout->count();
|
2015-06-05 00:30:24 +08:00
|
|
|
while (min < max)
|
|
|
|
{
|
2016-04-19 20:06:06 +08:00
|
|
|
int mid = (max - min) / 2 + min;
|
2016-09-23 10:06:20 +08:00
|
|
|
GenericChatItemWidget* atMid = qobject_cast<GenericChatItemWidget*>(layout->itemAt(mid)->widget());
|
2015-06-05 00:30:24 +08:00
|
|
|
assert(atMid != nullptr);
|
|
|
|
|
2015-06-06 01:54:55 +08:00
|
|
|
bool lessThan = false;
|
2015-06-19 00:01:30 +08:00
|
|
|
|
|
|
|
QCollator collator;
|
|
|
|
collator.setNumericMode(true);
|
|
|
|
|
|
|
|
int compareValue = collator.compare(atMid->getName(), widget->getName());
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-06 01:54:55 +08:00
|
|
|
if (compareValue < 0)
|
|
|
|
lessThan = true;
|
|
|
|
else if (compareValue == 0)
|
|
|
|
lessThan = atMid < widget; // Consistent ordering.
|
|
|
|
|
|
|
|
if (lessThan)
|
2015-06-05 00:30:24 +08:00
|
|
|
min = mid + 1;
|
|
|
|
else
|
|
|
|
max = mid;
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|