/* * QtColorSchemeComboBox.cpp * * Created on: 2013-5-27 * Author: long */ #include #include #include #include #include #include "qtColorSchemeComboBox.h" QtColorSchemeComboBox::QtColorSchemeComboBox(QWidget *parent):QtComboBox(parent), m_isShowText(true) { m_colorSchemeList.clear(); } QtColorSchemeComboBox::~QtColorSchemeComboBox() { freeColorScheme(); } void QtColorSchemeComboBox::ShowText(bool showText) { m_isShowText = showText; if(m_isShowText) { setMinimumWidth(140); } else { setMinimumWidth(40); setMaximumWidth(44); } } void QtColorSchemeComboBox::setSchemeColor(const QList &schemeList) { freeColorScheme(); for(int i=0;i &colorList,int colorNumber) { int w=64,h=32; QPixmap pixmap(w,h); QBrush brush(QColor(255,0,0)); pixmap.fill(QColor(255,0,0)); //QVector newColorList=paiInpolation(colorList,colorNumber); QPainter painter; painter.begin(&pixmap); float colorStep = colorNumber * 1. / w; QPen Pen; for (int x = 0; x < w; x++) { // 色标位置 int clrIndex = (int) ((x) * colorStep); if(colorList.size()<=x) break; Pen.setColor(QColor(colorList[clrIndex].color)); painter.setPen(Pen); painter.drawLine(x, 0, x, h-1); } painter.end(); return pixmap; } void QtColorSchemeComboBox::freeColorScheme() { m_colorSchemeList.clear(); } QVector paiInpolation(const QVector &colorList,int totalColorNum) { QVector newColorList; if (totalColorNum <= 1) totalColorNum = 2; // 填充新的颜色项目 for(int i=0;i=colorList.size()) index2=colorList.size()-1; float flgap1=pos-index1; float flgap2=index2-pos; int r=colorList[index1].color.red()*flgap2+colorList[index2].color.red()*flgap1; int g=colorList[index1].color.green()*flgap2+colorList[index2].color.green()*flgap1; int b=colorList[index1].color.blue()*flgap2+colorList[index2].color.blue()*flgap1; newColorList[i]=QtColorItem(QColor(r,g,b)); } } return newColorList; } bool readColorPaletteSetting(QList &schemeList,const QString &orignaName,const QString &moduleName) { schemeList.clear(); // 读取颜色设置 QString orgName=orignaName; if(orgName.isEmpty()) { orgName=gOrignizationName; } QString softName=moduleName; if(softName.isEmpty()) softName="General Color Schema"; QSettings settings(orgName, softName); QString aa=settings.fileName(); // 读取自定义的颜色方案 QVariant v = settings.value(gSchemeNumKey); int schemeNum=0; if(!v.isValid()) { return false; } schemeNum=v.toUInt(); // 循环读取颜色方案 QVector colorLis; for(int i=0;i(); item.strComments=list[1].toString(); colorLis.push_back(item); } QtSchemeColor schemeItem; schemeItem.schemeName=customName; schemeItem.isCustom=true; schemeItem.isDirty=false; schemeItem.colorList=colorLis; schemeItem.currentIndex=0; schemeList.push_back(schemeItem); settings.endGroup(); } return true; } int getPaiSystemColorNumber(QStringList &names) { QString strImagePath; QString strPathTmp = QCoreApplication::applicationDirPath() + QDir::separator(); strImagePath = QDir::toNativeSeparators( strPathTmp ); QString filecfg=strImagePath+"image"+QDir::separator()+"colortable"+QDir::separator()+"colortable.cfg"; FILE *fp1=fopen(filecfg.toStdString().c_str(),"rt"); if(!fp1) return -1; char buf[100]; QString str; int i=0; while(!feof(fp1)) { fscanf(fp1,"%s",buf); str=buf; if(str.indexOf("custom",Qt::CaseInsensitive)>=0) continue; names.push_back(str); i++; } fclose(fp1); return i; } int getPaiSystemColor(int nIndex,QVector &colorList,bool inpolation) { colorList.clear(); QtColorItem itemColor; QString strImagePath; QString strPathTmp = QCoreApplication::applicationDirPath() + QDir::separator(); strImagePath = QDir::toNativeSeparators( strPathTmp ); QString filecfg=strImagePath+"image"+QDir::separator()+"colortable"+QDir::separator()+"colortable.cfg"; FILE *fp1=fopen(filecfg.toStdString().c_str(),"rt"); if(!fp1) return -1; char buf[100]; QString str; int i=0; while(!feof(fp1)) { fscanf(fp1,"%s",buf); if(i>=nIndex) { str=buf; break; } i++; } fclose(fp1); if(str.isEmpty()) return -1; QString filename=strImagePath+"image"+QDir::separator()+"colortable"+QDir::separator()+str; FILE *fp=fopen(filename.toStdString().c_str(),"rt"); if(!fp) return -1; float r,g,b; QStringList strs; int colornum=0; while(!feof(fp)) { char *ret=fgets(buf,100,fp); if(!ret) break; str=buf; str.replace("\r",""); str.replace("\n",""); str.replace("\t"," "); if(str.indexOf("#")>-1) continue; if(str.indexOf("=")>-1) continue; if(str.indexOf("R",Qt::CaseInsensitive)>-1) continue; if(str.indexOf("G",Qt::CaseInsensitive)>-1) continue; if(str.indexOf("B",Qt::CaseInsensitive)>-1) continue; strs=str.split(" "); strs.removeAll(""); if(strs.size()!=3) continue; r=strs[0].toFloat(); g=strs[1].toFloat(); b=strs[2].toFloat(); if(g<=1||b<=1) if(r>0&&r<=1) r*=255; if(r<=1||b<=1) if(g>0&&g<=1) g*=255; if(r<=1||g<=1) if(b>0&&b<=1) b*=255; int rr=r,gg=g,bb=b; colorList.push_back(QtColorItem(QColor(rr,gg,bb))); colornum++; } fclose(fp); if(colornumPAITOTALCOLOR_MAX) colornum=PAITOTALCOLOR_MAX; // 最大总颜色数目 if(inpolation) { colorList=paiInpolation(colorList,colornum);//SYSTEM_INPOLATION); } return colorList.count(); } QtColorTableData *QtColorTableData::getInstance() { static QtColorTableData ref("Color Scheme", true); return &ref; } bool QtColorTableData::LoadScheme(bool IsReLoad) { if(!IsReLoad&&m_SharedMemory.isAttached()) { if(m_SharedMemory.key()!=m_modulName) { m_SharedMemory.detach(); m_SharedMemory.setKey(m_modulName); m_SharedMemory.attach(); } int s=m_SharedMemory.size(); if(m_SharedMemory.isAttached()&&s) { m_colorSchemeList.clear(); char *buffer=(char*)m_SharedMemory.data(); int size=*(int*)buffer; for(int i=0;i-1)name=name.mid(0,pos); QVector colorList; int ret=getPaiSystemColor(i,colorList,m_polatationSystemColor); if(ret<=0) continue; k++; // 构建颜色方案表 QtSchemeColor schemeItem; schemeItem.schemeName=name; schemeItem.colorList=colorList; schemeItem.isDirty=false; schemeItem.isCustom=false; schemeItem.currentIndex=0; m_colorSchemeList.push_back(schemeItem); // 对于系统颜色方案,单独生成备份 m_systemSchemeList.push_back(schemeItem); } m_systemShcemeNum=k; } // 读取定制的颜色方案 ReadSettings(); if(m_currentSchemIndex<0 || m_currentSchemIndex>=m_colorSchemeList.count()) { m_currentSchemIndex=0; } for(int i=0;icolorList.count()) return false; // 完成颜色插值 QVector newColorList= paiInpolation(scheme->colorList,colorNum); scheme->colorList=newColorList; if(scheme->currentIndex>=scheme->colorList.count()) scheme->currentIndex=0; ChangeDataRange(scheme); scheme->isDirty=true; return true; } QList QtColorTableData::GetRgb(int colornum) const { const QtSchemeColor* scheme=&m_colorSchemeList[m_currentSchemIndex]; QList rgbList; if(colornum<=0) colornum=scheme->colorList.count(); else if(colornum>SECTIONVIEW_NUM) colornum=SECTIONVIEW_NUM; QVector newColorList; if(colornum!=scheme->colorList.count()) newColorList=paiInpolation(scheme->colorList,colornum); else newColorList=scheme->colorList; for(int i=0;icolorList.count(); float flGap=(m_flMaxVal-m_flMinVal)/(colorNum); for(int row=0;rowcolorList[row].toValue=m_flMinVal+flGap; } else { scheme->colorList[row].fromValue=m_flMinVal+(row)*flGap; scheme->colorList[row].toValue=m_flMinVal+(row+1)*flGap; } } } void QtColorTableData::ReadSettings() { QList shcemeLis; QtColorItem itemColor; QString strImagePath; QString strPathTmp = QCoreApplication::applicationDirPath() + QDir::separator(); strImagePath = QDir::toNativeSeparators( strPathTmp ); QString filecfg=strImagePath+"image"+QDir::separator()+"colortable"+QDir::separator()+"colortable.cfg"; FILE *fp1=fopen(filecfg.toStdString().c_str(),"rt"); if(!fp1) return; char buf[100]; QString str,name; int i=0; while(!feof(fp1)) { fscanf(fp1,"%s",buf); name=buf; if(name.indexOf("custom",Qt::CaseInsensitive)>=0) { QString filename=strImagePath+"image"+QDir::separator()+"colortable"+QDir::separator()+name; FILE *fp=fopen(filename.toStdString().c_str(),"rt"); if(!fp) return; QVector colorList; float r,g,b; QStringList strs; while(!feof(fp)) { char *ret=fgets(buf,100,fp); if(!ret) break; str=buf; str.replace("\r",""); str.replace("\n",""); if(str.indexOf("#")>-1) continue; if(str.indexOf("=")>-1) continue; if(str.indexOf("R",Qt::CaseInsensitive)>-1) continue; if(str.indexOf("G",Qt::CaseInsensitive)>-1) continue; if(str.indexOf("B",Qt::CaseInsensitive)>-1) continue; strs=str.split(" "); strs.removeAll(""); if(strs.size()!=3) continue; r=strs[0].toFloat(); g=strs[1].toFloat(); b=strs[2].toFloat(); if((r>0&&r<=1)||(g>0&&g<=1)||(b>0&&b<=1)) { r*=255; g*=255; b*=255; } int rr=r,gg=g,bb=b; colorList.push_back(QtColorItem(QColor(rr,gg,bb))); } fclose(fp); QtSchemeColor schemeItem; name=name.mid(name.lastIndexOf("\\")+1); if(name.lastIndexOf(".rgb",-1,Qt::CaseInsensitive)>-1)name=name.mid(0,name.lastIndexOf(".rgb",-1,Qt::CaseInsensitive)); schemeItem.schemeName=name; schemeItem.colorList=colorList; schemeItem.isDirty=false; schemeItem.isCustom=true; schemeItem.currentIndex=0; m_colorSchemeList.push_back(schemeItem); } } fclose(fp1); return; // 读取 bool readOk=readColorPaletteSetting(shcemeLis,gOrignizationName, m_modulName); if(!readOk || shcemeLis.count()==0) return; for(int i=0;i=m_colorSchemeList.count()) return; m_currentSchemIndex=nIndex; } void QtColorTableData::FreeColorScheme() { m_colorSchemeList.clear(); m_systemSchemeList.clear(); m_SharedMemory.detach(); }