Skip to content

Commit

Permalink
put queries under timeframes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunderme committed Apr 15, 2024
1 parent 9ce3407 commit 62bab1d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/aichatassistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ void AIChatAssistant::onTreeViewClicked(const QModelIndex &index)
}else{
btInsert->setText(tr("Insert"));
}
}else{
// no query sent yet
textBrowser->clear();
}
}
/*!
Expand Down
73 changes: 71 additions & 2 deletions src/aiquerystoragemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ QVariant AIQueryStorageModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole) {
quintptr parent_row = index.internalId();
if (!parent_row) {
if(m_segments.size()>0){
return m_segments.at(index.row()).name;
}
return QStringLiteral("Queries");
} else {
if(m_segments.size()>0){
int previous=0;
if(parent_row>1 && parent_row<=m_segments.size()){
previous=m_segments.at(parent_row-2).index;
}
int r=index.row();
return m_files.value(previous+r);
}
return m_files.at(index.row());
}
}
Expand All @@ -32,17 +43,30 @@ QModelIndex AIQueryStorageModel::parent(const QModelIndex &index) const
if (row == 0) {
return QModelIndex{};
} else {
return createIndex(row, 0);
return createIndex(row-1, 0);
}
}

int AIQueryStorageModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return 1; // for now
if(m_segments.size()>0){
return m_segments.size();
}
return 1; // nothing recorded yet
} else {
quintptr row = parent.internalId();
if (row == 0) {
if(m_segments.size()>0){
int row=parent.row();
if(row>0){
int previous=m_segments.at(row-1).index;
int current=m_segments.at(row).index;
return current-previous;
}else{
return m_segments.at(row).index;
}
}
return m_files.size();
}
return 0;
Expand All @@ -66,6 +90,32 @@ void AIQueryStorageModel::setStoragePath(const QString &path)
{
m_storageDirectory.setPath(path);
m_files=m_storageDirectory.entryList({"*.json"},QDir::Files,QDir::Reversed);
if(m_files.isEmpty()) {
return;
}
auto lst_names = std::vector{tr("Today"),tr("Last Week"),tr("Last Month")};
auto lst_date = std::vector{QDate::currentDate(),QDate::currentDate().addDays(-7),QDate::currentDate().addMonths(-1)};
auto last_it=m_files.cbegin();
for(size_t i=0;i<lst_date.size();++i){
QString date = lst_date.at(i).toString("yyyyMMdd");
auto it=std::upper_bound(m_files.constBegin(),m_files.constEnd(),date,std::greater<QString>());
if (it-last_it > 0) {
TimeFrame tf;
tf.name=lst_names.at(i);
tf.index=it-m_files.constBegin();
m_segments.append(tf);
last_it=it;
}
if(it==m_files.cend()){
break;
}
}
if(last_it!=m_files.cend()){
TimeFrame tf;
tf.name=tr("Older");
tf.index=last_it-m_files.constBegin();
m_segments.append(tf);
}
}

QString AIQueryStorageModel::getFileName(const QModelIndex &index) const
Expand All @@ -74,12 +124,31 @@ QString AIQueryStorageModel::getFileName(const QModelIndex &index) const
if (row == 0) {
return QString{};
}
if(m_segments.size()>0){
int previous=0;
if(row>1 && row<=m_segments.size()){
previous=m_segments.at(row-2).index;
}
int r=index.row();
return m_storageDirectory.absoluteFilePath(m_files.value(previous+r));
}
return m_storageDirectory.absoluteFilePath(m_files.at(index.row()));
}

void AIQueryStorageModel::addFileName(const QString &name)
{
beginInsertRows(QModelIndex{},m_files.size(),m_files.size());
m_files.prepend(name);
for(auto &tf:m_segments){
++tf.index;
}
if(m_segments.size()>0){
if(m_segments[0].name!=tr("Today")){
TimeFrame tf;
tf.name=tr("Today");
tf.index=1;
m_segments.prepend(tf);
}
}
endInsertRows();
}
8 changes: 8 additions & 0 deletions src/aiquerystoragemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class AIQueryStorageModel : public QAbstractItemModel
QDir m_storageDirectory;
QStringList m_files;

struct TimeFrame
{
QString name;
int index;
};
QList<TimeFrame>m_segments;


// QAbstractItemModel interface
};

Expand Down

0 comments on commit 62bab1d

Please sign in to comment.